Re: [Tkinter-discuss] Focus issue
To answer my own question: the focus was being given to another widget after the code in question had executed. That said, I still don't understand why focus_set followed by update_idletasks followed by focus_get would return None, but tkinter something works in strange and mysterious ways. Cheers Cam On 2024-01-10 08:44, Cam Farnell wrote: In a rather large program I have this code: self.TextWidget.focus_set() print("type(self.TextWidget)=%s"%type(self.TextWidget)) print('Focus_get()=%s'%self.focus_get()) The goal is to have TextWidget get the focus. Usually it does, but there is one particular circumstance when it does not get the focus. The "print" lines I added for debugging. The output from the print functions is: type(self.TextWidget)= Focus_get()=None TextWidget is an instance of rpWidgets.ColorizedText which is created thus: class ColorizedText(tkinter.Text): which is to say, it's just a tkinter.Text to which I've added methods. I've tried creating a minimum example without success. My questions is: how is it possible for TextWidget to NOT have the focus immediately after a call to focus_set? Changing focus_set to focus_force makes no difference. Invoking update_idletasks() after the focus_set makes no difference. The TextWidget in question is visible on the screen, and pressing Tab a couple of times does get it the focus. Having spent some hours chasing this I'm a tad frustrated, but quite open to suggestions about what to look for. Cheers Cam Farnell ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Focus issue
In a rather large program I have this code: self.TextWidget.focus_set() print("type(self.TextWidget)=%s"%type(self.TextWidget)) print('Focus_get()=%s'%self.focus_get()) The goal is to have TextWidget get the focus. Usually it does, but there is one particular circumstance when it does not get the focus. The "print" lines I added for debugging. The output from the print functions is: type(self.TextWidget)= Focus_get()=None TextWidget is an instance of rpWidgets.ColorizedText which is created thus: class ColorizedText(tkinter.Text): which is to say, it's just a tkinter.Text to which I've added methods. I've tried creating a minimum example without success. My questions is: how is it possible for TextWidget to NOT have the focus immediately after a call to focus_set? Changing focus_set to focus_force makes no difference. Invoking update_idletasks() after the focus_set makes no difference. The TextWidget in question is visible on the screen, and pressing Tab a couple of times does get it the focus. Having spent some hours chasing this I'm a tad frustrated, but quite open to suggestions about what to look for. Cheers Cam Farnell ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Text: immutable strings
Hi Vasilis, I've done this successfully. Bind an event handler to the KeyPress event of the text. Check the tags and if they are inside immutable text and the key is something that would change the text the return 'break' which will stop the processing of the key. Cheers Cam Farnell On 2018-10-26 12:12 p.m., Vasilis Vlachoudis wrote: Hi all, I have a Text() widget and I want to display some values tab formatted. The values should be prefixed with a text-label which is immutable by the user something like label: valuelabel: value the user should be able to edit the values but not the labels. I know that I can insert the label as a window create_window(Label()) but for very big files it becomes extremely slow If I tag the labels, can I forbid the Text widget to disallow the text editing over this specific tag? Thanks in advance Vasilis ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Help related to tkinter on Mac
Hello All, I've developed a simple flowcharting program in Python 2.7 + Tkinter which I am about to release as open source. All the development was done on an Xubuntu box so it works under Linux and, thanks to the magic of VirtualBox, I've tried it successfully under a couple of version of Windows. Under Macs it is more problematic. What I've found so far is: - If you run it under the Python 2.7 that comes with the machine then you get a message about it not finding a config file, which is normal for first-time startup, but then it hangs without even starting the GUI and there is no way to interrupt it to see what is going on. - If you run it under Python 2.7 which was installed with the package manager "brew" (https://brew.sh/) then it works. Neither of the people who tried if for me on their Macs know much about Python, and I certainly don't know much about Macs. Therefore I'm hoping that someone on this list who has a Mac and knows something about Python might be willing to give it a try. Based on that I could make changes or at least update the web site to let Mac users know what the situation is. The web site with information and the download link is: http://www.retroflow.ca Many thanks Cam Farnell Halifax, NS, Canada ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Dealing with a Tk reference
I've been using New Mexico Tech example code from here: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/menu-toplevel.html to create a top-level menu. The example code is: top = self.winfo_toplevel() self.menuBar = tk.Menu(top) top['menu'] = self.menuBar self.subMenu = tk.Menu(self.menuBar) self.menuBar.add_cascade(label='Help', menu=self.subMenu) self.subMenu.add_command(label='About', command=self.__aboutHandler) and that works just fine. In particular it creates a Menu using: self.menuBar = tk.Menu(top) and then assigns it to the top-level using: top['menu'] = self.menuBar In my application it is not convenient to keep the reference to the Menu in self.menuBar. Later when it comes time to add another another cascade to the menu I was hoping to be able access the menu bar using: menuBar = top['menu'] but in fact having done that, menuBar not a tk.Menu object as I was hoping but rather a string, such as ".3070843372L", which is presumably a reference to a tk object. So my question is: how to use the string to access the corresponding tk.Menu object? There's probably some magic way to do that but a bunch of internet searching didn't turn it up. Thanks Cam ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Tkinter Text Undo
Am I missing something here? I was trying the Tkinter Text widget's undo feature. It works, sort of, but if for example you delete some text then when you undo the delete the text comes back but all it's tags are gone. Really? If that's how it actually works and not just me being stupid, it makes the undo feature almost totally useless. Could someone who knows more about this than me shed some light on this? Python 2.7.3 on Ubuntu Linux. Thanks Cam Farnell ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Standard colors
Hi Emiliano, Thanks for your reply. Yes they are documented, but per my post what I am looking for is a PROGRAMATIC way that my application can get the list of colors, sort of like how in Python you can use dir(__builtin__) to get a list of all the built-in functions. Cheers Cam Farnell On 14-02-16 01:33 PM, Emiliano Gavilan wrote: El 16/02/2014 14:03, Cam Farnell ms...@bitflipper.ca mailto:ms...@bitflipper.ca escribió: Hello all, Tkinter supports a bunch of standard colors, for example something like: MyButton = Button(activebackground='red') works and somebody, somewhere, knows that 'red' means #FF'. What I'm trying to find out is if there is a programatic way of getting a list of the colors that are available. Anyone know this offhand? A quick look through Tkinter.py was not overly illuminating. I'm using Python 2.7.2 and whatever version of tk/tcl came with Ubuntu 12.04 LTS. They are documented. http://www.tcl.tk/man/tcl8.6/TkCmd/colors.htm Regards ___ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] DRAG and DROP image
You might check out this page: http://www.bitflipper.ca/Documentation/Tkdnd.html which gives a description of how to implement drag and drop in tkinter. On 12-09-03 02:28 AM, rhixz06 wrote: hey guys!hmm... I'm kinda new in tkinter...my friends and I were developing an educational game,..I just want to ask if you know an easiest way to drag and drop an image in tkinter please I need a reply thanks,.. ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] [REQ] Tkinter programmer
RTFM On 12-03-04 02:27 PM, fresher wrote: yeah... but if i am able to do that i wont ask. so now I am asking for help or for full project. thank you. ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Button-press on Menubuttons
I have a bunch of Menubuttons in my application, lets call them A, B and C. Each Menubutton is bound to ButtonPress-1 so, before the menu choices are displayed, I can enable/disable some of those choices depending on the situation at the time, and each binding leads to its own Menubutton-specific handler. If the user clicks on Menubutton-A then the corresponding handler runs as expected. If, without making a choice from menu A, the user then clicks on Menubutton-B then - and this is the nub of the problem - the handler bound to Menubutton-B does NOT run. If the user now clicks on Menubutton-C, then the handler for B, yes B, runs. Is this a bug, a feature or me being stupid? I'm running Python 2.6.5 on Ubuntu 10.04. The simplest demonstration example I could manage follows. Clicking on Menubutton A shows, in the label to the right of the Menubuttons, that the handler ran. If you then click immediately (without selecting any of the dummy menu choices or otherwise making the open menu go away) on Menubutton B then the handler does not run. If you then click on Menubutton A (or for that, click again in Menubutton B) then the handler for B runs. I've already developed a workaround for my particular application, but this strikes me as anomalous behavior. Cheers Cam Farnell import Tkinter as tk class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.pack() self.MenuButtonA = tk.Menubutton(self,text='Menu Button A') self.MenuButtonA.pack(side='left') T = tk.Menu(self.MenuButtonA) T.add_command(label='Dummy 1') T.add_command(label='Dummy 2') self.MenuButtonA['menu'] = T self.MenuButtonA.bind('ButtonPress-1',self.HandlerA) self.MenuButtonB = tk.Menubutton(self,text='Menu Button B') self.MenuButtonB.pack(side='left') T = tk.Menu(self.MenuButtonB) T.add_command(label='Dummy 1') T.add_command(label='Dummy 2') self.MenuButtonB['menu'] = T self.MenuButtonB.bind('ButtonPress-1',self.HandlerB) self.InfoLabel = tk.Label(self,text='-') self.InfoLabel.pack(side='left') def HandlerA(self,Event): self.InfoLabel['text'] = 'Handler A' def HandlerB(self,Event): self.InfoLabel['text'] = 'Handler B' app = Application() app.master.title(Sample application) app.mainloop() ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Recommendations on when to use pack vs. grid layouts?
Another option is to use Rapyd-TK (http://www.bitflipper.ca/rapyd/). It only uses pack but it makes it trivially easy to add additional frames and it also makes it really easy to rearrange the layout of your widgets, something not easy with pack or grid by themselves. Cheers Cam Farnell pyt...@bdurham.com wrote: Are there any best practice tips regarding when one should use pack vs. grid for their layouts? From what I've been reading via google, the concencus seems to be that grid can handle any pack scenario but not vice-versa. Thanks, Malcolm ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Angled text on canvas.
Hmm. Need to draw text on an angle on a canvas but no mention of angled text in the documentation. Does anyone know if there is actually a way to do this or is it time to implement a vector font and do it the hard way? Thanks Cam Farnell ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Desperate GUI Help Please!!
Thomas, A post which in essence says please write my application for me is pretty much a guaranteed way to annoy people - who are generally busy writing their own applications - or to simply be ignored. A better approach would be to figure out what part of Tkinter isn't working as you expect, construct the *smallest* possible code that demonstrates the problem (hint: this will NOT be an entire section of your application) and submit that to this list. Cheers Cam thomas1984 wrote: Hello, I need to make a GUI which has a fixed window size approx 390 (width) X 6020 (height) It needs to have a fileMenu containing File About Exit. In the file menu it needs a menuItem called new route. When you click on new route it needs to display a new window with a listBox.. When a user selects an item from the list box and clicks OK - it should pass this value to the original window. The difficulty here is that the main body of the GUI window, needs to display an image BUT it needs to be an image that is able to be drawed on. Below is my code for drawing on the image... It is an EXAMPLE - but i need it like this i.e. by importing the ImageDraw library. # Do the proper imports for this script from Tkinter import * import os, sys import Image import ImageDraw # Load the image try: im = Image.open(C:\Documents and Settings\Administrator\Desktop\mm.gif) except: print Unable to load image exit(1) # Get a drawing surface for the image draw = ImageDraw.Draw(im) # Draw the circle #draw.ellipse((0,0)+im.size) # Draw the line #draw.line((0,0)+im.size) # and show off the result draw.line((0, 0) + (0,55), fill=000, width=22) draw.text((50, 50), hey) im.show() Below is my code for the list box example.. from Tkinter import * class ListBoxTest : def __init__(self) : self.root = Tk() self.list_box_1 = Listbox(self.root) self.list_box_1.pack() self.delete_button = Button(self.root, text=Delete, command=self.DeleteSelection) self.delete_button.pack() def DeleteSelection(self) : items = self.list_box_1.curselection() pos = 0 for i in items : idx = int(i) - pos self.list_box_1.delete( idx,idx ) pos = pos + 1 def Show(self) : for i in range(0, 10) : s = Item + str(i) self.list_box_1.insert( END,s ) self.root.mainloop() lbt=ListBoxTest() lbt.Show() Below is my code for the Canvas and Window BUT the canvas does not hold an image I have drew on from Tkinter import * class AppUI(Frame): def __init__(self, master=None): Frame.__init__(self, master, relief=SUNKEN, bd=2) self.menubar = Menu(self) menu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=File, menu=menu) menu.add_command(label=New) menu = Menu(self.menubar, tearoff=0) self.menubar.add_cascade(label=Edit, menu=menu) menu.add_command(label=Cut) menu.add_command(label=Copy) menu.add_command(label=Paste) try: self.master.config(menu=self.menubar) except AttributeError: # master is a toplevel window (Python 1.4/Tkinter 1.63) self.master.tk.call(master, config, -menu, self.menubar) canvas = Canvas(width = 300, height = 200, bg = 'white') canvas.pack(expand = YES, fill = BOTH) gif1 = PhotoImage(file = 'mm.gif') canvas.create_image(50, 10, image = gif1, anchor = NW) canvas.pack() root = Tk() app = AppUI(root) app.pack() root.mainloop() Is there anyone here who can put these elements together and maybe make this GUI for me? I'm really stuck and as you can see by the code examples I have gave this a shot, but I don't know how to finish it off - If someone can do this for me - it would be great as I can look at the code and learn. ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Need help on gui focus l
The code given exhibits the problem on my Ubuntu Linux box. For the sake of clarity I enumerate three windows involved in this code: - The MAIN window which has a button marked popup in it. - The CHILD windows which has a button marked filedialog in it. - The OPEN window which is the tkFileDialog On my machine clicking on the popup and filedialog buttons works ok, but once you select a file and click OPEN, then what is on top and visible is the MAIN window whereas what the original poster wanted was to see the CHILD window. Even worse, if (starting from scratch again) you click the popup button, then drag the resulting CHILD window to a slightly new position then click the filedialog button, the OPEN window appears *under* the CHILD window which seems less than useful. Cam Farnell Michael Lange wrote: I cannot reproduce this behavior here (linux), maybe it is a platform specific problem ? I tried the follwing which works for me as expected: ### CODE ### from Tkinter import * import tkFileDialog root = Tk() top = Toplevel() top.withdraw() top.protocol('WM_DELETE_WINDOW', top.withdraw) def filedialog(): print tkFileDialog.askopenfilename() b0 = Button(top, text='filedialog', command=filedialog) b0.pack(padx=100, pady=100) def popup(): top.deiconify() b0.focus_set() b1 = Button(root, text='popup', command=popup) b1.pack(padx=100, pady=100) root.mainloop() ### CODE ### In case I just did not understand exactly your problem, could you post a short code snippet here that shows the problem and tell us which platform you are using, please ? Michael ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] non-GUI event detection in Tkinter mainloop()
The only way I know of to do this is to use after to invoke a function which does the file checking and whatever else is required. That function sets another after - with perhaps a one second delay or whatever seems appropriate - to invoke itself. Thus the function is invoked repeatedly to keep track of the file. It isn't pretty but it works. Cam Farnell O'Gorman, Lawrence (Larry) wrote: I’m trying to figure out if Python and Tkinter can offer a solution to my following problem. I want to monitor a file and when that file is changed, I want to detect that event and have my window change its text from “File Unchanged” to “File has Changed.” That is, I can produce a function to periodically sample the modification time of a file and if it is different than my last sample time, then this function will return an indication that the file is changed. I want this file-change event to be captured by the tkinit mainloop(), which I will then use to change the appearance of my window. I understand that any GUI event (mouse, keyboard, etc.) can be detected in the mainloop(), but I have seen no indication in the documentation and discussions that a file-change can produce an event that can be detected in the mainloop(). Does anyone know if I can do this? If so, how? Or, if there is an alternative way than mainloop() to get a window to change upon a file-change, that would be helpful, too. ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] Kinetic Scrolling and Keyboard Shortcuts in Menus
I ran into exactly this keyboard shortcut issue while writing the Rapyd-Tk Python development environment (http://www.bitflipper.ca/rapyd/). My solution is based on these functions, where the left part is the usual menu label and the right part is the shortcut. If the length of the shortcut text varies a lot you can compute the width on the fly as is done in Rapyd, otherwise just use some reasonable constant. def PadToWidth(LeftPart,RightPart,Width,Widget): Given left and right parts, return string of specified width in pixels. o LeftPart is the string to be left justified in the result. o RightPart is the string to be right justified in the result. o Width is the desired width, in pixels, of the result. o Widget the calculation is done with reference to the font set for this widget. Note that since we pad with space characters, the result will be as close as possible to the target size but will not necessarily be exactly the number of pixels requested. UsedPixels = TextMeasure(LeftPart+RightPart,Widget) PadPixels = Width - UsedPixels PadCount = int(round(float(PadPixels) / float(TextMeasure(' ',Widget return '%s%s%s'%(LeftPart,' '*PadCount,RightPart) def TextMeasure(Text,Widget): =u Measure size of Text, in pixels, if displayed in Widget. return int(Widget.tk.call(font, measure, Widget[font] ,-displayof, Widget.master, Text )) Johnston Jiaa wrote: Also, I have set keyboard shortcuts for some functions in my program. The functions these shortcuts call are also linked to menu items in the menu bar. How can I get the menu labels to reflect the keyboard shortcuts for each relative command? I know I can set the shortcut as a string in the label manually, but it will be stupid, not flush with the right edge of the menu, like in other programs. Thanks for helping my program not be stupid ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] simple way of clearing out a text widget
Well one of the best ways is to read the manual first. Some quite reasonable TkInter documentation is available here: http://infohost.nmt.edu/tcc/help/pubs/tkinter/ You might look for documentation on the Text widget and then Methods on Text widgets. The Text widget is pretty good at dealing with text; I'm sure there will be some way of getting rid of unwanted text. Cam Alexnb wrote: I am wondering, what is the best way to clear out a text widget. If it matters, I will still want to use that text widget again. ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] how to search a directory from a Tkinter program
A good start would be to read the documentation for the Python OS module. Cam Farnell brindly sujith wrote: hi i am developing an application in tkinter i want to know whether we have any option to search a directory from tkinter program please answer me thank you ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
Re: [Tkinter-discuss] having trouble, newbie
1) Use grid_columnconfigure and grid_rowconfigure to create a dummy extra row and column beyond the columns and rows you actually use and set weight to 1. The dummy row and column then soak up any extra space and the actual widgets float up the the top left. It took me a while to figure this out. Any column/row index greater than what you actually use will do. 2) Use geometry to set the window size. 3) use focus_set. There is good tkinter documentation at this URL: http://infohost.nmt.edu/tcc/help/pubs/tkinter/ although it doesn't tell you about the columnconfigure trick. Here's a simple example: from Tkinter import * Root = Tk() Root.geometry('640x480') Root.grid_rowconfigure(index=10,weight=1) Root.grid_columnconfigure(index=10,weight=1) BB = Button(text='HelloWorld') BB.focus_set() BB.grid() Root.mainloop() Cam Farnell inhahe wrote: I'm trying to make a simple app in tkinter and am having two problems 1, i can't figure out how to put widgets in the top left of the main window. no matter what i try with row, column and sticky parameters it puts my buttons in the middle of the window. 2, i pass width=800 and height=600 to Frame.__init__ and my window still comes up tiny, just barely big enough for two buttons. oh, while i'm at it--how do I make it so when the the window is created it gets focus, like a ... real app? ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss
[Tkinter-discuss] Tkinter slows down
I'm creating a program which displays spreadsheet-like data using hundreds of labels. At first this works fine, however after a few redisplays it starts slowing down dramatically. A small test program, without the distractions of the real program is: from Tkinter import * def Go(): LabelList = [] for J in range(30): print J for L in LabelList: L.grid_forget() LabelList = [] for Row in range(20): for Col in range(20): L = Label(r,text='%s'%J) L.grid(row=Row,column=Col) LabelList.append(L) r.update_idletasks() r = Tk() r.after(1000,Go) r.geometry('800x600') r.mainloop() This runs quickly at first but gradually slows down to a crawl during the latter iterations. Anyone have any idea what is causing this to go slow or how I could speed it up? I'm running Python 2.4.3 on an Ubuntu 6.06 system. Thanks Cam Farnell ___ Tkinter-discuss mailing list Tkinter-discuss@python.org http://mail.python.org/mailman/listinfo/tkinter-discuss