This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 9 months ago.
I have three dropdown menus and 3 string variables that will take on the value of the menus, However in the function that I define query, it seems as if the string variables do not take on the value each time I select a new one on the dropdown menu. It will print the set value and do nothing else. Also what's weird is that it was working perfectly and then suddenly didn't work at all.
Really need help on this one thanks.
root=Tk()
root.title('Query')
root.geometry('600x400')
def query():
print(clicked_id_mouse.get())
print(clicked_id_filename.get())
print(clicked_id_researcher.get())
clicked_id_mouse=StringVar()
clicked_id_mouse.set("Choose Mouse Id")
drop_mouse= OptionMenu(root,clicked_id_mouse,*mouse_id)
drop_mouse.place(x=50,y=150)
clicked_id_researcher=StringVar()
clicked_id_researcher.set("Choose Researcher")
researcher= OptionMenu(root,clicked_id_researcher,*r_i)
researcher.place(x=200,y=150)
clicked_id_filename=StringVar()
clicked_id_filename.set("Choose Filename")
filename= OptionMenu(root,clicked_id_filename,*filename)
filename.place(x=350,y=150)
button= Button(root,text="Query",command=query())
button.place(x=210,y=200)
root.mainloop()
When you are assigning a command to a button use "lambda: ...". Otherwise it will execute it only once the button is created. Your code should work as intended by changing
button= Button(root,text="Query",command=query())
into
button= Button(root,text="Query",command=lambda: query())
I hope that helps.
Related
This question already has an answer here:
Tkinter assign button command in a for loop with lambda [duplicate]
(1 answer)
Closed 2 years ago.
Here is my code:
buttons=[]
for i in range(100):
buttons.append(Button(parent,text="0",command=lambda:[change(i)])
def change(i):
buttons[i]["text"]="1"
but as finally, the i will go to 99, I could only change the last button no matter which button I clicked. So I wonder is there any good way to do so?
The solution is very easy
Just change the command by below command
command = lambda i=i:change(i)
This will do the job for you.
Your solution not worked as expected because
python passes the last value of i
And hence to resolve this problem
The above command is the solution.
Here lambda is given argument(that is i) at the same time when each execution of loop is going and then that i
is passed to change function which is why it worked
This question already has answers here:
Setting focus to specific TKinter entry widget
(2 answers)
Closed 2 years ago.
I am currently working on some code, these functions create a new text box when the enter key is hit, and tab the text box out when the tab key is hit. This is all working well, as can be seen in the functions below, but I would like to be automatically clicked into the text box as soon as it is created so I can immediately start typing, rather than having to click into it manually and then start typing, as this becomes quite slow, especially with the tab function where new boxes are deleted and created a few times. I was wondering if anyone knew a way to automatically put the cursor into the text box, or whether that is possible?
def makeLine(self,event):
self.x=0
self.newbox=Text(self.root,width=20, height=3)
self.newbox.place(x=self.x, y=self.y)
self.newbox.bind('<Tab>', self.increaseCol)
self.newbox.bind('<Return>', self.makeLine)
self.y=self.y+55
def increaseCol(self, event):
event.widget.destroy()
self.x=self.x+30
self.tabbedbox=Text(self.root, width=20, height=3)
self.y=self.y-55
self.tabbedbox.place(x=self.x, y=self.y)
self.tabbedbox.bind('<Tab>', self.increaseCol)
self.y=self.y+55
self.tabbedbox.bind('<Return>', self.makeLine)
Thank you!
(moving comment to answer)
You can use focus() or focus_set()to set the focus on a widget:
self.newbox.focus() # also try focus_set()
http://effbot.org/tkinterbook/entry.htm
Setting focus to specific TKinter entry widget
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 3 years ago.
I am trying to get a button to do some stuff in Python (using Tkinter). I want the button to get the current choice of a combobox and print it on screen.
The graphical layout is 3 separate frames, but both the button and the combobox reside in the same frame (frame #2).
The problem is that i cannot refer to the combobox. The errors i am getting read:
Frame object has no attribute 'box'
Window object has no attribute 'box'
self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command= self.wipe(), text=...)
def wipe(self):
self.box.get()
ALTERNATIVELY i tried:
def wipe(self):
self.frame2.box.get()
The goal is to simply get the selected choice from the Combobox.
HERE IS MINIMAL CODING THAT PRODUCES THE SAME ERROR:
import tkinter as tk
from tkinter import ttk
class window():
def __init__(self,root):
self.frame=tk.Frame(root)
self.key=tk.Button(self.frame,text='PRESS ME',command=self.wipe())
self.box=ttk.Combobox(self.frame, options=['1','2','3'])
self.frame.pack()
self.key.pack()
self.box.pack()
def wipe(self):
self.box.get()
master=tk.Tk()
master.geometry('400x400')
app=window(master)
master.mainloop()
I would add the tag "tkinter" to the question.
Try the following:
def wipe(self):
# code
self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command=wipe, text=...)
Notice the following:
I first defined wipe, only then used it.
I am not quite sure why you wanted to do command=self.wipe(), as there are two issues here. First, you are setting command to the result of self.wipe() which is NOT a function. Second, you haven't defined self.wipe, you defined wipe.
command=wipe Sets the command keyword argument to the
function wipe
Its been long since I dealt with tkinter, if this doesn't work, I'll try to help by checking the docs again.
This question already has answers here:
Why is my Tk button being pressed automatically?
(2 answers)
Closed 8 years ago.
I'm using Python and TkInter to build a GUI where a certain command is called when a button is pressed. The relevant code looks like this:
class Main(Frame):
def __init__(self, parent):
self.generate_noise_button = Button(root, text="Add noise and save", command=self.generate_noise())
...
def generate_noise(self):
header.generate_noise()
If I place a breakpoint in the generate_noise() method and run the program, the breakpoint is instantly hit despite never being called or the button being pressed. Furthermore whenever I actually do press the button the method is never called making the button completely useless. Why?
self.generate_noise_button = Button(root, text="Add noise and save", command=self.generate_noise())
When you provide a command argument, you're not supposed to add parentheses to the end.
self.generate_noise_button = Button(root, text="Add noise and save", command=self.generate_noise)
You want the command to refer to the method object, not the value that the method returns after it is called.
This question already has answers here:
Getting a callback when a Tkinter Listbox selection is changed?
(3 answers)
Closed 9 years ago.
I have a listbox on a GUI in Tkinter. I would like to implement a routine where if a listbox item is selected a function is called (based on this selection) to modify the gui (add another adjacent listbox). Then if that selection changes, the gui reverts back to its default view. Can this be done? Seems you would need to associate a function to a listbox selection, not sure how to do this or if its possible... Does anyone have the secret?
Its possible to add "select" buttons to the bottom of my listbox, but I wanted to avoid this extra work for user and save space on the GUI.
Thanks to all in advance! Daniel
The listbox will fire the virtual event <<ListboxSelect>> whenever the selection changes. If you bind to it, your function will be called whenever the selection changes, even if it was changed via the keyboard.
Ok nevermind, the link below answers my question with the example below:
http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
from Tkinter import *
root = Tk()
def callback(event):
print "clicked at", event.x, event.y
frame = Frame(root, width=100, height=100)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
(replace frame with listbox widget of course)
Works !