This question already has an answer here:
Set default caret position inside entry widget in Tkinter Python
(1 answer)
Closed 3 years ago.
My GUI is structured such that I have a main class, and each page of my GUI, of which I can navigate between, is instantiated whenever I call it. One of my pages has an entry box, but i have to manually move my cursor and select the entry box to begin typing. Is there a way for the entry box to automatically be selected when I call that page?
Seems like an interesting problem
You should use focus().
For Example, if your entry widget is e. Then use e.focus().
Related
This question already has answers here:
Is there a way to clear all widgets from a tkinter window in one go without referencing them all directly?
(3 answers)
Closed 5 months ago.
I am creating a program with TKinter and am wondering if there is a more efficient way to delete widgets from the window.
I am aware you can delete them 1 by one with:
widget_name.forget()
is there a more efficient and less tedious way to delete them? Like a delete all command?
As has been suggested, if the widgets in question are contained in a parent element like a Frame, "forgetting" the parent will also forget its children.
Another option: if the widgets in question share a parent, you can try the following
# this is useful when you want to keep 'parent' but not its children
# just replace 'parent' with whatever widget's children you want to remove
for widget in parent.winfo_children()
widget.forget()
This question already has an answer here:
Detect if user has clicked the 'maximized' button
(1 answer)
Closed 1 year ago.
I'm currently working on this project and was wondering is there a way to resize all the children widgets present inside the parent widget when the user maximize the parent widget root, I know about .grid_rowconfigure()and .grid_columnconfigure(), but using these methods isn't changing the fontsize/actual size of my widgets.
I would like to know if there's a way to bind the maximize button
present on the top right conner of the parent widget.
Thank you.
This question has been answered here. The solution: First of all, we need to bind the window to the <Configure> event. The <Configure> event gets triggered whenever a window event occurs.
window.bind("<Configure>", is_maximized)
Now, when the event gets triggered, the is_maximized function will be called:
def is_maximized(event):
if window.state() == "zoomed":
# your code
In the function, we are checking if the window state is equal to zoomed. If so, it means that the window is maximized. Now, you can just add whatever code you need inside the if clause.
This question already has an answer here:
How to bind self events in Tkinter Text widget after it will binded by Text widget?
(1 answer)
Closed 2 years ago.
I am actually making a text editor and obviously I needed text coloring for that. It works perfectly, as I was using it in a thread, which makes it lag a lot. I think if I bind it with every key, it'll work, so when any key is pressed (except spacing keys like ENTER, TAB, BACKSPACE, etc), it'll check if a python keyword is on the screen and change its color.
I am looking for something like this:
textArea.bind("<NonSpacingKeys>", color_coding)
Okay so I read this: How to bind self events in Tkinter Text widget after it will binded by Text widget?
textArea.bind("<KeyPress>", color_coding)
this worked prefectly
This question already has answers here:
Creating menus using wxpython
(2 answers)
Closed 7 years ago.
When I create a button in wxpython I can do:
self.all = wx.Button(self, -1, _("&ALL"))
or
self.all = wx.Button(self, -1, _("ALL"))
both generate a button named ALL
So, what is the meaning of the & in the name parameter?
ducomntation doesn't specify anything about it.
the question here is similar but it's not the same. The solution there is set focus. while you can set focus to a menu, you can not set focus to a button.
I think the & indicates the keyboard shortcut that can be used to activate the button. So &All means you can press A instead of clicking on the All button.
You can put the & anywhere in the label, the character after it becomes the shortcut. For instance, one of the stock labels on the documentation page is:
wx.ID_CUT 'Cu&t'
This means that T is the shortcut for the Cut button.
I guess (keyword: GUESS) is since you are building a GUI, you may want to consider internationalisation link. This is done by searching through your *.py file and search for any string that could be replaced to a different language, using UNICODE. By having & in front of the string displaying your button, you can easily search for it using, maybe, gettext.
Again, a guessing answer :)
This question already has answers here:
binding to cursor movement doesnt change INSERT mark
(2 answers)
Closed 8 years ago.
I'm making a basic IDE, line numbers are going to be similar to IDLE, but I don't want to bind each possible key to an event which changes the box with the current line/col in it. Is there some kind of "on change", or "one cursor move" event build into Tkinter, or more specifically, ScrolledText. If there isn't then if anyone can point me in the right direction that would be fantastic.
Thanks!
There's nothing built-in per se, but what you want to do is possible if you're willing to be creative.
At it's core a text widget is a tcl command, and this command is called whenever something happens to the text widget: text is inserted, deleted, the cursor changes, etc. The nature of tcl is that we can replace this command with our own command. And since we can do that, we can detect certain changes, and call our own function before or after.
It sounds complicated, and it is. On a positive note, it's foolproof once you have it working, and it means you don't have to do any custom bindings. To see a complete working example, see this answer to the question binding to cursor movement doesnt change INSERT mark.
The scrolled text widget is just a thin wrapper around a regular text widget, so this answer will work with just a tiny bit of tweaking (you'll need the reference to the text widget used by the scrolledtext widget). The wrapper is so thin, however, that I recommend not using it since adding scrollbars to a text widget is trivial.