Can't tie command to button Python 3.7 [duplicate] - python

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.

Related

Tkinter stringvar dropdown menu [duplicate]

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.

Tkinter Label doesn't work as I thought it does [duplicate]

This question already has an answer here:
When should I use root.update() in tkInter for python
(1 answer)
Closed 1 year ago.
Beginner programmer here currently trying to learn Tkinter for a school assignment.
I have a GUI class that stores the Tkinter labels etc, the labels are innitiated like this:
# GUI for Player 1
self.player_1_name_field = Label(
self.root,
text="Player 1",
font=GUI_Settings.player_information_font,
anchor=W,
background=GUI_Settings.playerfield_active_color
)
I then create a Game() object that looks like this:
class Game():
def __init__(self):
self.GUI = GUI()
self.GUI.initializeBoard()
self.GUI.root.mainloop()
When I run the code, the labels do get created and are where they are supposed to be, but are completely black. Once I move or resize the window it instantly becomes how I want it to be, it just behaves weird when at the start of the code
The interesting thing is that I also have a Canvas and a List that work perfectly fine, only the Labels are not cooperative
If you need further info, just ask for it!
Thank you!
Edit 1: I have a function called drawWindow() that redraws the chessboard when I re-configure the window. In the init of the GUI class I set self.root.bind("<Configure>", self.drawWindow). If I remove that line of code, the Labels work but the Canvas doesn't anymore. I'm so confused. For anyone wanting to take a look at my tiny code: https://codeshare.io/DZYzyZ
See comment of Thingamabobs
The issue is self.root.update(). Remove this line and you'll be fine.
When should I use root.update() in tkInter for python.
This works but you shouldn't do it
This is a tricky issue. Your problem come from the bind of the configure event. Bind to the root window, it is applied to all sub-widgets of the window, which cause the bug (I don't know why yet).
This will solve your issue (line 202):
self.chessboard.bind("<Configure>", self.drawWindow)
instead of:
self.root.bind("<Configure>", self.drawWindow)
Result without moving or resizing the window:
I found the information here (french forum).

How to move cursor into a newly created text box upon creation in tkinter? [duplicate]

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

Python 3.4.2 tkinter Menu Auto Calling Function [duplicate]

This question already has answers here:
All tkinter functions run when program starts
(2 answers)
Closed 7 years ago.
I'm very new to python's tkinter gui and I am trying to use it to build a basic test.
I created the menu where one of the menu items must call a function although when I run the program I can see the output from the function before the menu item has been clicked and when the menu item is clicked it does not call the function.
My code is as follows
from tkinter import *
class cl_main():
def __init__(self, master):
lo_mainmenu = Menu(master)
lo_mainmenu.option_add('*tearOff', FALSE)
master.config(menu=lo_mainmenu)
lo_menugroup = Menu(lo_mainmenu)
lo_mainmenu.add_cascade(label="MenuGroup")
lo_menugroup.add_command(label="Command", command=f_message())
def f_message():
print ("This Function Has Been Called")
root = Tk()
co_main = cl_main(root)
root.mainloop()
I can't see what is wrong with it but I'm sure there is something horribly wrong here
lo_menugroup.add_command(label="Command", command=f_message())
callbacks shouldn't have parentheses. As it is, f_message is called right away and its return value is assigned to command, rather than the function object itself.
lo_menugroup.add_command(label="Command", command=f_message)

Calling a function based on a Listbox current selection "curselection()" in Tkinter [duplicate]

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 !

Categories

Resources