There are multiple entry widgets in my tkinter gui. So I want to use a single function to enter data in the entry widget currently in focus with the help of a keypad of buttons. How can I do it?
You can use root.focus_get() to get the current focused widget, derive StringVar/IntVar and update them properly, or just use entry.insert('end','0') for example.
Related
I am making a physics calculator that involves splitting and organizing my calculator by topic. this means i have lots of widgets to navigate the menu to get to the problem. i don't want to delete the widgets as this would remove all the details of the widgets which would make my code unnecessarily complicated and long. is there a way to dynamically link the variables or widgets so that the program can run more smoothly?
I do not know of any way to do this so i cannot provide any code. If there is no way to do it i would also appreciate knowing this.
a way to dynamically link the variables or widgets so that the program can run more smoothly?
I suggest taking look at StringVar which might be linked with suitable widgets, consider following simple example
from tkinter import Tk, Label, Entry, StringVar
root = Tk()
str_var = StringVar(root,"hello")
label = Label(root, textvariable=str_var)
entry = Entry(root, textvariable=str_var)
label.pack()
entry.pack()
root.mainloop()
This creates window with Label and Entry, as you change text in Entry corresponding change is made to Label. You might also find .trace method of StringVar instance useful as it allows you to register callback i.e. function to be called when value was changed.
I am using Tkinter for my python script. At current stage, gui looks ugly because of too many options/widgets in one screen. Not all of them are needed all the time and it can be divided into four parts. Hence my idea is to have them expand the frame (one frame for each part) with its options/widgets in the current window.
Is it possible in tkinter to expand the window/frame when a button or a checkbox is clicked?
ps - I have tried opening each part in new window but this makes gui unnecessarily complicated.
Thanks!
So, your gonna need to define what happens when the button is clicked-
def buttonClicked():
root.geometry("350x300")
where root is the window and then on the button you will need to write -
btn = tkinter.Button(text = "Button", command=buttonClicked)
btn.pack
Hope that answers your question!
I have multiple buttons in my tkinter 8.5 GUI (on Windows 7). I want whatever button is focused on (tabbed over) to be selected when the user hits Enter. I know I have to bind '<Return>', but I need the rest of the gaps filled in.
Thanks in advance!
Assuming you want this to be universal to all applications in the root window you could do something similar to this.
def clickButton():
widget = root.focus_get()
if widget != root:
widget.invoke()
root = Tkinter.Tk()
root.bind("<Return>", clickButton)
root.mainloop()
That will run any command associated with the currently tabbed selection. If you want to limit it to certain buttons you can do type-checking inside of the method. Widget will be whatever widget is currently in focus via the tabbed selection. Also beware of a user hitting enter on certain widgets that may not support the invoke method.
I am using Tkinter to create an application which requires a 0-9 numerical keypad to be built in to the UI.
I plan to do this with 10 button widgets which enter the relevant number(s) into the currently selected Entry widget.
I do not want to use one of the pre-made on-screen keyboards (e.g. Matchbox-keyboard) that are available, it needs to be bespoke to the application.
So essentially - how do I simulate key-press events using on-screen buttons to enter values into entry fields without taking the focus off the entry field?
You don't need to simulate keypresses if all you want to do is insert the numbers into the entry widget. Just have the buttons directly insert their value into the entry widget with the entry widget insert method.
I have a PyQt GUI set up that has a selection of QPushButtons and a QLineEdit text box (among other things). The text box is set up so as to call a function upon returnPressed(). My problem is that when I click on the text box and put in text, one of the buttons becomes selected which means that when I press enter in the text box it activates both the button and the text box function.
Is there a way around this? Some way to stop any buttons from being selected while the text box is being edited?
The code is fairly long so I can't add it here but if there are any questions regarding layout or anything that may be relevant, please ask.
Thank you for any help you can offer
From your question and comments, I'm guessing that the buttons and line-edit are in a QDialog, and that the selection/highlighting occurs due to the default/autoDefault property of buttons.
Normally, these properties will be set to False, but in a QDialog they are automatically set to True. The button that is the current default gets an additional frame drawn around it (even when it doesn't have the keyboard focus), and is activated whenever the return key is pressed.
You can of course prevent this behaviour by simply doing:
button.setDefault(False)
button.setAutoDefault(False)
for each button in the dialog.