This question already has an answer here:
How do I create child windows with Python tkinter?
(1 answer)
Closed 6 years ago.
When you run this code and try and check the the check button and click the button to print out the value of the check button it does not work. I can't figure out why. It prints out 0 whether checked or unchecked.
from tkinter import *
def awesome():
def click_me():
print(var.get())
return
root = Tk()
root.title("a good try")
var = IntVar()
x = Checkbutton(root, text = "check me", variable = var)
y = Button(root, text = "click me", command = click_me)
x.pack()
y.pack()
root.mainloop()
return
def main():
main = Tk()
cool = Button(main, text = "click", command = awesome)
cool.pack()
main.mainloop()
main()
change root = Tk() to root = Toplevel()
need to use Toplevel() for a window that opens on another window.
Related
This question already has answers here:
Tkinter: AttributeError: NoneType object has no attribute <attribute name>
(4 answers)
Closed 1 year ago.
I am having a trouble changing text in python even though though that I saw a lot of use it
My code:
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text).pack()
window.mainloop()
You assigned the return value of .pack() to btn, and pack doesn't return the widget it was called on (it returns None implicitly, since it has no useful return value). Just split up the creation of the button from the packing:
from tkinter import *
window = Tk()
def change_text():
btn.config(text="second text")
btn = Button(window, text="first text", command=change_text)
btn.pack()
window.mainloop()
your code is good but it is not good to make .pack() in the same line
from tkinter import *
window = Tk()
def change_text():
btn.config(text = "second text")
btn = Button(window ,text= "first text" , command= change_text )
btn.pack()
window.mainloop()
it works just will
I am new to programming and Tkinter. I want to DISABLED textbox when checkbox is pressed and open it NORMAL when box is not ticked. Here is my code:
from tkinter import *
root = Tk()
def lock_fields():
if check == True:
data.configure(state=DISABLED)
if check == False:
data.configure(state=NORMAL)
check = BooleanVar()
open_for_edit = Checkbutton(root, text="check the box for editing", variable=check,
onvalue=True, offvalue=False, command=lambda: lock_fields())
open_for_edit.pack()
check = check.get()
data = Text(root)
data.insert(END, "I would like to be able to edit this text only when checkbox is checked.")
data.pack()
root.mainloop()
It seems that for some reason the check-variable is always False when it enters to lock_fields function. I tried passing check argument to the method.
You're pretty close, only thing is that the check.get() line must be in the function. Also you don't need the lambda. Try this:
from tkinter import *
root = Tk()
def lock_fields():
if check.get():
data.configure(state=DISABLED)
else:
data.configure(state=NORMAL)
check = BooleanVar()
open_for_edit = Checkbutton(root, text="check the box for editing", variable=check, onvalue=True, offvalue=False, command=lock_fields)
open_for_edit.pack()
data = Text(root)
data.insert(END, "I would like to be able to edit this text only when checkbox is checked.")
data.pack()
root.mainloop()
I'm new to python and I'm trying to create an application in which I want a button to be visible only after I click the "show" button. The button should not be visible from the start of application it should only be visible after clicking on "show" button.
I have this code which hides the button after clicking on another button. It changes its text to "show" after hiding the button.
from tkinter import *
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()
I want the button to show only after I click on the "show" button, not from the start.
At the risk of stating the obvious, if you don't want the button to be visible at startup, don't show the button at startup.
You're explicitly making the button visible with this line of code near the start of the program:
btn1.place(x=20, y=50)
You're then setting btn1.pi by calling place_info, but you don't need to do that. You can directly set btn1.p without first calling .place followed by place_info.
btn1 = Button(root,text="Example")
btn1.visible = False
btn1.pi = {"x": 20, "y": 50}
Notice that I also changed btn1.visible to False. You don't actually need a separate attribute to track if it's visible, tkinter can answer that question with the method winfo_viewable().
Or, you can simply remove the button after calculating btn1.pi:
btn1.place(x=20, y=50)
btn1.visible = False
btn1.pi = btn1.place_info()
btn1.place_forget()
I know this is not for the question I had a hard time finding this. If you are using the GTK library then
ButtonName.set_visible(False) sets it invisible
the below is for tkinter I just tested the below for tkinter:
from tkinter import *
def hide_me(event):
event.widget.pack_forget()
def make_invisible(widget):
widget.pack_forget()
root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
make_invisible(btn2)
root.mainloop()
the make_invisible(btn2) method sets it invisible however make sure to call it after you pack the button otherwise it will still show up.
here is my resources I used to get to this code
https://www.tutorialspoint.com/how-to-make-a-tkinter-widget-invisible
and https://newbedev.com/in-tkinter-is-there-any-way-to-make-a-widget-not-visible
i hope that this is what you mean:
from tkinter import *
import tkinter
root = Tk()
btn1 = Button(root,text="Example")
btn1.visible = True
btn1.place(x=20, y=50)
btn1.pi = btn1.place_info()
btn3 = Button(root, text="click me", command=lambda:plugin())
btn3.place(x=20, y=150)
def plugin():
master = Tk()
def toggle1():
if btn1.visible:
btnToggle1["text"] = "Show Example"
btnToggle1["state"] = DISABLED
print ("Now you don't")
btn1.place_forget()
else:
btn1.place(btn1.pi)
print ("Now you see it")
btnToggle1["state"] = NORMAL
btnToggle1["text"] = "Hide Example"
btn1.visible = not btn1.visible
btnToggle1 = Button(master, text="Hide Example", command=toggle1)
btnToggle1.place(x=70, y=150)
master.mainloop()
root.mainloop()
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 5 years ago.
I'm trying to make my program change the text based on a variable selected in the dropdown menu, but the button to activate the command doesn't seem to be working. From what I can see, the select function run's once the program is loaded and then never again, regardless of when I click the button.
from Tkinter import *
class App:
def __init__(self, root):
self.title = Label(root, text="Choose a food: ",
justify = LEFT, padx = 20).pack()
self.label = Label(root, text = "Please select a food.")
self.label.pack()
self.var = StringVar()
self.var.set("Apple")
food = ["Apple", "Banana", "Pear"]
option = apply(OptionMenu, (root, self.var) + tuple(food))
option.pack()
button = Button(root, text = "Choose", command=self.select())
button.pack()
def select(self):
selection = "You selected the food: " + self.var.get()
print(self.var.get()) #debug message
self.label.config(text = selection)
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
I'm a beginner on Tkinter, and I'm trying to figure out the basics before I go into making my full app. Thanks in advance :)
Try changing button = Button(root, text = "Choose", command=self.select()) to button = Button(root, text = "Choose", command=self.select). Note the removed parentheses after self.select. This way, the method only gets referenced and not actually executed until you press the button.
Your main issue is you don't need the parentheses when setting command=self.food():
button = Button(root, text="Choose", command=self.select)
As a side-note, the way you generate your OptionMenu is slightly unusual. You can use the following instead, which is more consistent with the rest of your code:
option = OptionMenu(root, self.var, *food)
The command parameter documentation as per tkinterbook.
(A function or method that is called when the button is pressed. The callback can be a function, bound method, or any other callable Python object. If this option is not used, nothing will happen when the user presses the button.)
*****************************modified code*******************************
from Tkinter import *
class App:
def __init__(self, root):
self.title = Label(root, text="Choose a food: ",
justify = LEFT, padx = 20).pack()
self.label = Label(root, text = "Please select a food.")
self.label.pack()
self.var = StringVar()
self.var.set("Apple")
food = ["Apple", "Banana", "Pear"]
option = apply(OptionMenu, (root, self.var) + tuple(food))
option.pack()
button = Button(root, text = "Choose", command=self.select)
#use function name instead of aclling the function
button.pack()
def select(self):
selection = "You selected the food: " + self.var.get()
print(selection) #debug message
self.label.config(text = selection)
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
I have a bit of difficulty with the code below. Basically, I want the code to, when I press the Enter button, to open the window2 but also close window1 simultaneously so that there is only one window and not two of them.
The code is...
from tkinter import *
def window1():
window = Tk()
window.title("Welcome")
f = Frame()
f.pack()
label1 = Label(window, text = "Welcome to the random window")
label1.pack()
button1 = Button(window, text = "Enter...", command = window2)
button1.pack()
def window2():
screen = Tk()
screen.title("Pop-Up!")
fr = Frame()
fr.pack()
label2 = Label(screen, text = "This is a pop-up screen!")
label2.pack()
button2 = Button(screen, text = "Return", command = window1)
button2.pack()
window1()
This is "Bad" because you're using two instances of Tk. Try instead using TopLevels.
import tkinter as tk
def window1():
window = tk.Toplevel(root)
window.title("Welcome")
# etc etc ...
tk.Button(window,text="Enter...",command=lambda: window2(window)).pack()
def window2(old_window):
old_window.destroy()
# window2 stuff
root = tk.Tk()
root.iconify() # to minimize it, since we're just using Toplevels on top of it
window1()
root.mainloop()
When you are using the Tk() function, you are creating a new instance of the Tcl/tkinter interpreter. Instead use Toplevel() which will make a new window in the current interpreter.