Hello I'm obviously not too experienced with tkinter very much and I couldnt find anything on what I was looking for, maybe someone could help me
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!" command=hide(d))
d.pack()
I would like it so that on click the command runs but the button is not defined when calling the command
You can not use anything if you are still building. Must you use configure and lambda function:
from tkinter import *
def hide(x):
x.pack_forget()
root = Tk()
d=Button(root, text="Click to hide me!")
d.configure(command=lambda: hide(d))
d.pack()
root.mainloop()
First, define the button, then add the command with the config method.
from tkinter import *
root = Tk()
def hide(x):
x.pack_forget()
d=Button(root, text="Click to hide me!")
d.pack()
d.config(command=lambda: hide(d))
root.mainloop()
Related
I am trying to learn how to use Tkinter, but whenever I want to execute my code I always get this problem: (NameError: name 'label' is not defined) or (NameError: name 'button' is not defined).
As a beginner, it seems to me that the problem is with my code editor. (BTW I am using VScode)
this is my code:
from tkinter import *
root = Tk()
mylabel = label(root, text='Hello World')
mylabel.pack()
root.mainloop()
And as I said, this also happens with this one:
from tkinter import *
root = Tk()
mybutton = button(root, text='Hello World')
mybutton.pack()
root.mainloop()
you have caps error its Button and Label not button and label
I have found many questions in stackoverflow regarding how to add a custom title bar in tkinter. It works for me, but none of the answers show how to add a minimise and maximise buttons on it. I am using a mac computer, so here is the code -
from tkinter import *
root = Tk()
root.geometry("1000x740")
root.overrideredirect(1)
root.overrideredirect(0)
Button(root, text="Close", command=root.quit).pack()
root.mainloop()
How can I add a minimise and maximise button in this?
You need to call root.wm_state('iconic')
import tkinter as tk
class My_Window(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry("300x300")
tk.Button(
self, text="Minimize",
command=lambda: self.wm_state('iconic')
).pack()
if __name__ == '__main__':
app = My_Window()
app.mainloop()
I created a button that works perfectly (not entire code here) but I want that once you press the 'Save' button, the window disappear. Someone knows how to do it?
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_parameter(ents1))
save_button.pack()
root2.mainloop()
Based on the extremely limited snippet of code in your question: I would suggest it doing by defining a function to call that does something like this:
import tkinter as tk
def ask_and_close(root, ents):
ask_parameter(ents)
root.destroy()
ents1 = "something"
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_and_close(root2, ents1))
save_button.pack()
root2.mainloop()
Note: If you're creating multiple windows, I wouild suggest using tk.Toplevel() instead of calling tk.TK() more than once.
Just use the master.quit() method!
Example Code:
from tkinter import *
class Test:
def __init__(self):
self.master = Tk()
self.button = Button(self.master, text="Push me!", command=self.closeScreen)
self.button.pack()
def closeScreen(self):
# In your case, you need "root2.quit"
self.master.quit()
test = Test()
mainloop()
I would suggest using destroy() method as used here https://docs.python.org/3.8/library/tkinter.html#a-simple-hello-world-program.
One of the easy ways to invoke the destroy method in your code is this;
def ask_parameter_and_destroy(ents1):
ask_parameter(ents1)
root2.destroy()
root2 = tk.Tk()
root2.geometry('200x100')
save_button = tk.Button(root2)
save_button.configure(text='Save', command=lambda: ask_parameter_and_destroy(ents1))
save_button.pack()
root2.mainloop()
You can read about differences between destroy() and previously proposed quit() methods on the following page: What is the difference between root.destroy() and root.quit()?.
If your goal is to create a dialog for saving a file, you might be interested in tkinter.filedialog library, which has ready made dialog boxes for handling file saving.
I am very noob and I need help please.
Here's my code:
from tkinter import *
root = Tk()
root.geometry('500x500')
def callback() :
print ("click!")
b = Button(root, text="OK", command=callback)
b.pack()
mainloop()
Try This:
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry('200x60')
root.title('Test Application')
def notify():
tkMessageBox.showwarning('Notification', 'Display is not capable of DPMS!')
b1 = Button(root, text="Run!", command=notify)
b1.pack()
root.mainloop()
Uri, you should probably run it on your computer instead of on the web. If you are using a chromebook go into settings and enable Linux.
——-Windows-——
Windows Install:
Click here
——-Mac-——
Mac Install:
click here
——-Linux-——
Linux Problems:
click here
Linux Install:
click here
I need to activate many entries when button is clicked
please do not write class based code, modify this code only because i need to change the whole code for the project as i did my whole project without classes
from Tkinter import *
import ttk
x='disabled'
def rakhi():
global x
x='NORMAL'
root = Tk()
frame=Frame(root)
frame.pack()
entry1=Entry(frame,state=x)
entry1.pack()
entry2=Entry(frame,state=x)
entry2.pack()
button1=Button(frame,text="press",command=rakhi)
button1.pack()
mainloop()
You need to use the configure method of each widget:
def rakhi():
entry1.configure(state="normal")
entry2.configure(state="normal")