simpledialog or filedialog are widgets very convenient to use.
I would like to do the same :
modal window which pops up on screen like these simpledialogs
combo box inside
and when I select a value in combo, return this value without needing a button
Something like:
def askComboValue():
root = Tk() #how to pops up this window?
label = ttk.Label(root, text = "select your value")
label.pack()
box_value = ''
combo = ttk.Combobox(root, textvariable=box_value, values=['bla', 'bli', 'blo'])
combo.current(0)
combo.pack()
combo.bind("<<ComboboxSelected>>", returnValue) #how to catch this value?
root.grab_set_global() #is it the right way to make it modal?
root.mainloop()
return box_value #how to return this value?
Does anyone know how to deal with it?
Thanks for your help
If the function is called when there is already a tkinter window, then better use Toplevel() instead of Tk(). Also box_value should be instance of StringVar() instead. grab_set() is used instead of grab_set_global() as well.
Below is an example based on your code:
import tkinter as tk
from tkinter import ttk
def askComboValue(*values):
top = tk.Toplevel() # use Toplevel() instead of Tk()
tk.Label(top, text='Select your value').pack()
box_value = tk.StringVar()
combo = ttk.Combobox(top, textvariable=box_value, values=values)
combo.pack()
combo.bind('<<ComboboxSelected>>', lambda _: top.destroy())
top.grab_set()
top.wait_window(top) # wait for itself destroyed, so like a modal dialog
return box_value.get()
def test():
result = askComboValue('bla', 'bli', 'blo')
print(result)
root = tk.Tk()
tk.Button(root, text='Test', command=test).pack()
root.mainloop()
Related
I have a following question. I want to make a button in tkinter that will delete existing changes and the window will looks like the initial window.
This is my initial Window 1:
This is how the window looks like when I click on the first two buttons, Window 2:
Now I would like to click on the "Zpět" button and I want to see Window 1 again.
Here is my code:
import tkinter as tk
root = tk.Tk()
home_frame = tk.Frame(root)
home_frame.grid(row=0, column=0, sticky="news")
def raise_new_payment():
tk.Label(text=f"Stav bilance k 2021-09-09").grid()
def back():
"""I would like to this function to clean everything."""
tk.Label().destroy()
platba = tk.Button(
home_frame,
text="Zadej novou platbu",
command=lambda: raise_new_payment(),
)
platba.pack(pady=10)
zpet = tk.Button(
home_frame,
text="Zpět",
command=back,
)
zpet.pack(pady=10)
I don't know how to use the back() function. I tried to delete the tk.Label as created in raise_new_payment(), but it did not work. Can you help me please? Thanks a lot.
I would suggest you create the label once and don't call .pack() on it first, i.e. it is not visible initially.
Then update it inside raise_new_payment() and call .pack() to show it.
You can call .pack_forget() to hide it again inside back().
import tkinter as tk
root = tk.Tk()
home_frame = tk.Frame(root)
home_frame.grid(row=0, column=0, sticky="news")
def raise_new_payment():
# update label and show it
lbl.config(text=f"Stav bilance k 2021-09-09")
lbl.pack()
def back():
# hide the label
lbl.pack_forget()
platba = tk.Button(
home_frame,
text="Zadej novou platbu",
command=lambda: raise_new_payment(),
)
platba.pack(pady=10)
zpet = tk.Button(
home_frame,
text="Zpět",
command=back,
)
zpet.pack(pady=10)
# create the label and initially hide it
lbl = tk.Label(home_frame)
root.mainloop()
from tkinter import *
root = Tk()
label = Label(root, text="Name:")
label.pack()
e = Entry(root)
e.pack()
root.mainloop()
How do I get a user input in entry widget without button in tkinter?
window = tk.Tk()
username = tk.Entry(window)
username.pack()
def it_has_been_written(*args):
#what you want to execute when user starts typing something
pass
username.trace_add("write", it_has_been_written)
window.mainloop()
To see more about this https://tkdocs.com/tutorial/widgets.html
I would like to write a tkinter app that will automatically update a value based on the current state of the OptionMenu object. Here's what I have so far
from tkinter import *
root = Tk()
def show():
myLabel=Label(root,text=clicked.get()).pack()
clicked=StringVar()
clicked.set("1")
drop = OptionMenu(root,clicked,"1","2","3")
drop.pack()
myButton = Button(root,text="show selection",command=show)
root.mainloop()
In this version, the text can only be updated by clicking a button. How can I make the text update automatically, without this "middle man"?
You can simply assign clicked to the textvariable of the Label, then whenever an option is selected, the label will be updated:
import tkinter as tk
root = tk.Tk()
clicked = tk.StringVar(value="1")
drop = tk.OptionMenu(root, clicked, "1", "2", "3")
drop.pack()
tk.Label(root, textvariable=clicked).pack()
root.mainloop()
After changing some things, i got it working.
It is better to use the config() function to change item's attributes, and another important thing is to not pack() the objects (the Label, in this case) in the same line that the variable declaration.
Like so, you'll be able to change the text. Here is your code updated!
from tkinter import *
def show():
myLabel.config(text = clicked.get())
root = Tk()
clicked=StringVar( value="1")
myLabel=Label(root, text="click the button at the bottom to see this label text changed")
myLabel.pack()
drop = OptionMenu(root, clicked, "1","2","3")
drop.pack()
myButton = Button(root, text="show selection", command=show)
myButton.pack()
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.
I want NewWinButton to create only one new window at a time, which means if
if NewWin.winfo_exists() == 1:
NewWinButton.config(state='disabled')
else:
NewWinButton.config(state='normal')
I can make this work if I add a button to the new window (QuitButton in this example):
import tkinter as tk
root = tk.Tk()
root.title('Main Window')
root.geometry('400x400')
def get_new_win():
NewWin = tk.Toplevel(root)
NewWin.title('New Window')
NewWin.geometry('300x300')
NewWinButton.config(state='disable')
def quit_win():
NewWin.destroy()
NewWinButton.config(state='normal')
QuitButton = tk.Button(NewWin,text='Quit', command=quit_win).pack()
NewWinButton = tk.Button(root,text='New Window', get_new_win).pack()
root.mainloop()
This works if and only if I use QuitButton to close the new window; however, if I use the close button in the new window, then the NewWinButton will remain 'disabled'.
Can anyone tell me how to fix this?
Use NewWin.protocol("WM_DELETE_WINDOW", quit_win) to assign function quit_win to the close button.
import tkinter as tk
root = tk.Tk()
root.title('Main Window')
root.geometry('400x400')
def get_new_win():
NewWin = tk.Toplevel(root)
NewWin.title('New Window')
NewWin.geometry('300x300')
NewWinButton.config(state='disable')
def quit_win():
NewWin.destroy()
NewWinButton.config(state='normal')
QuitButton = tk.Button(NewWin, text='Quit', command=quit_win)
QuitButton.pack()
NewWin.protocol("WM_DELETE_WINDOW", quit_win)
NewWinButton = tk.Button(root, text='New Window', command=get_new_win)
NewWinButton.pack()
root.mainloop()
BTW:
The pack() method returns None, not a button instance:
NewWinButton = tk.Button(...).pack()
use this:
NewWinButton = tk.Button(...)
NewWinButton.pack()