I have a code where I have a drop down menu and what I need to do is that when I select an entry from the drop down list (ex: Send an email) and press on go, I need this to populate another tk window (child window).
I know I am doing something wrong but can not comprehend how to overcome this, I have been searching for a while but I am unable to find a solution or guidance on how to complete this.
Thanks in advance for your help with this!
from tkinter import *
root = Tk()
root.geometry("400x100")
#========================================
#Entry area to enter the number
labelmain = Label(root,text="Please enter number:")
labelmain.pack()
entryvar = StringVar(root)
entrymain = Entry(root, textvariable=entryvar,width=30)
entrymain.pack()
#========================================
#Create option drop down list:
lst = ["Save details to DB", "Send an email", "Copy format", "email", "View report"]
ddl = StringVar(root)
ddl.set(lst[0])
option = OptionMenu(root, ddl, *lst)
option.pack()
#========================================
#Function to get the values from drop down list
def ok():
print("value is: " + ddl.get())
#root.quit()
#=========================================
#Button to process the selection:
btnmain = Button(root,text="Go", command=ok)
btnmain.pack()
#=========================================
if ddl.get() == "Send an email":
samepmrdb = Tk()
samepmrdb.mainloop()
root.mainloop()
You are checking the value of ddl right after you open up your window. As you said in your question, you want some stuff happen after pressing the button so you need to put those codes under the command of said button.
Also, a tkinter app should only have one Tk() instance and one mainloop. When you want to open another window, you should use Toplevel().
def ok():
print("value is: " + ddl.get())
if ddl.get() == "Send an email":
samepmrdb = Toplevel()
#now you can populate samepmrdb as you like
If all you're looking to do is find a way to update a second tkinter window with the selection from an OptionMenu on the first tkinter window this can be achieved easily using the below code:
from tkinter import *
class App:
def __init__(self, master):
self.master = master
self.top = Toplevel(master)
self.master.withdraw()
self.var = StringVar()
self.var.set("Example1")
self.option = OptionMenu(self.top, self.var, "Example1", "Example2", "Example3", "Example4")
self.button = Button(self.top, text="Ok", command=lambda:self.command(self.var))
self.label = Label(self.master)
self.option.pack()
self.button.pack()
self.label.pack()
def command(self, var):
self.master.deiconify()
self.label.configure(text=var.get())
self.label.pack()
root = Tk()
app = App(root)
root.mainloop()
This creates a Toplevel widget which contains an OptionMenu and Button widget. The Button widget will then output the selection from the OptionMenu widget when pressed.
This kind of logic can be used for all sorts of things and it's relatively simple to pass information from one window to another, provided this is what your question is asking.
Related
Here is my code:
from tkinter import *
OPTIONS = ["Available","Busy","Invisible","Away"]
now = Toplevel()
variable = StringVar(now)
variable.set(OPTIONS[0]) # default value
details = {"U_status":""}
def verify():
global u_status
details["U_status"]=variable.get()
print ("value is:" + variable.get())
now.destroy()
def status():
w = OptionMenu(now, variable, *OPTIONS)
w.pack()
button = Button(now, text="OK", command=verify, relief='flat')
button.pack()
if __name__=='__main__':
status()
mainloop()
While running the above code, along with the window (I wanted) another empty window appears. Can anyone figure out what is wrong in this code?
Here now = Toplevel() should be replaced with Tk(), like:
now = Tk()
When you use Toplevel() a Tk() window is made in the background, if its not already made(your case), and that is the reason you are getting a blank new window. Actually that blank window is your main window.
Toplevel() is used to make child windows for the parent Tk() windows,ie, if you want sub windows within your main window(now), you will use Toplevel(). Because more than one Tk() in your code will cause some errors later on.
The blank window is actually the root window of your app that tkinter creates by default. You probably want to be explicit, and create a tk.Tk() root, and keep a reference to it.
New windows can be spawned and destroyed at leisure; your app will continue to exist as long as you keep the root active.
Maybe something like this:
import tkinter as tk
def verify():
now = tk.Toplevel(root)
details["U_status"] = variable.get()
txt = f'value is: {details["U_status"]}'
tk.Label(now, text=txt).pack()
now.after(3000, now.destroy)
def status():
tk.OptionMenu(root, variable, *OPTIONS).pack()
tk.Button(root, text="OK", command=verify, relief='flat').pack()
if __name__=='__main__':
OPTIONS = ["Available", "Busy", "Invisible", "Away"]
root = tk.Tk()
variable = tk.StringVar(root)
variable.set(OPTIONS[0])
details = {"U_status": ""}
status()
root.mainloop()
I'm barely new in Python, so forgive me if my codes aren't written in a correct 'pythonic way'...
I want to save the user's choice from a listbox. The function is defined in the BES_library module (I need the function to stay here in the module and not in the script!):
##BES_library
def quit(root, listbox, choice):
#get the user's choice
choice = listbox.get(listbox.curselection())
print(choice)
#quit the ListBox
root.destroy()
root.quit()
return choice
And this is my script:
import tkinter as tk
import BES_library as BES_lib
if __name__ == '__main__':
SheetsNames = ['Foglio1', 'Foglio2', 'Foglio3']
SheetName = 'lalala'
root = tk.Tk()
root.title("Select the worksheet to be opened")
listbox = tk.Listbox(root, selectmode= 'single')
for item in SheetsNames:
listbox.insert("end", item)
listbox.pack()
button = tk.Button(root, text='Select', command= lambda: BES_lib.quit(root, listbox, SheetName) )
button.pack()
root.geometry("450x300+120+120")
root.mainloop()
print(SheetName)
#now I want to work with the SheetName chosen by the user
The user is supposed to select one of the options from the listbox and click the 'Select' button. Upon clicking the button, I want to attribute his choice to a variable as a string and quit the listbox.
The question is, how can I use what the user entered? The button doesn't save the return value of functions.
I tried also this code, but nothing seems to be changed:
import tkinter as tk
import BES_library as BES_lib
if __name__ == '__main__':
SheetsNames = ['Foglio1', 'Foglio2', 'Foglio3']
root = tk.Tk()
root.title("Select the worksheet to be opened")
SheetName = tk.StringVar()
listbox = tk.Listbox(root, selectmode= 'single')
for item in SheetsNames:
listbox.insert("end", item)
listbox.pack()
button = tk.Button(root, text='Select', command= lambda: BES_lib.quit(root, listbox, SheetName) )
button.pack()
root.geometry("450x300+120+120")
root.mainloop()
print(SheetName.get())
#now I want to work with the SheetName chosen by the user
Here's a method that eliminates the need to use the quit method, at all. Your quit method didn't make sense, anyway. It claims to remove the listbox but it destroys the entire root of the app. You also have a misconception ~ you are trying to print something after calling root.mainloop. Nothing you do after root.mainloop is ever going to get hit. root.mainloop needs to be the last line of your program.
Using my method, The listbox and button are placed in a frame. Every time a user clicks on the listbox it stores the selection. Calling sheetframe.pack() shows the selection frame and clicking select unpacks/hides it. In this way, select isn't selecting anything. The selection was already recorded as soon as a listbox item was clicked.
import tkinter as tk
#init root
root = tk.Tk()
root.title("Select the worksheet to be opened")
root.geometry("450x300+120+120")
#init vars
sheetlist = ['Foglio1', 'Foglio2', 'Foglio3']
sheetname = 'lalala'
#container for listbox and button
sheetframe = tk.Frame(root)
listbox = tk.Listbox(sheetframe, selectmode='single', listvariable=tk.StringVar(value=sheetlist))
listbox.pack()
#unpack/hide sheetframe on click
tk.Button(sheetframe, text='Select', command=sheetframe.pack_forget).pack()
#store a listbox selection a soon as it is clicked
def selection(event):
global sheetname
sheetname = listbox.get('anchor')
print(f'{sheetname} selected')
#bind on ButtonRelease so anchor is definitely set
listbox.bind('<ButtonRelease-1>', selection)
#show sheetframe
sheetframe.pack()
root.mainloop()
So i am making a password organisator in python, and i don't know how i can get user input from an Entry and use it in an if argument?
text1 = StringVar()
def but():
text1.get()
print(text1.get())
knapp2 = Button(root, command="but").pack()
entry1 = Entry(root, textvariable=text1).place(x=270, y=100)
You can call the .get() function on on the Entry widget too to get the text.
import tkinter
from tkinter import Tk, Button, Entry
mw = Tk()
entry = Entry(mw)
entry.pack()
def but():
text = entry.get()
print(text)
button.config(text='Button Clicked')
button = Button(mw, command=but, text='Test')
button.pack()
mw.mainloop()
This code does work but will become complicated with larger code. You will have to define the function before creating a widget that calls that function. In the above example if you created the button widget before the function you would get an exception. You could create the widget, then create the function, then change the configuration of the button to call that function when clicked but that's still pretty complicated and will be confusing in large programs.
I would recommend putting everything in a class. It makes it easy to reference widgets in functions.
import tkinter
from tkinter import Tk, Button, Entry
class Main:
def __init__(self, master):
self.master = master
self.entry = Entry(self.master)
self.entry.pack()
self.button = Button(self.master, text='Test', command=self.But)
self.button.pack()
def But(self):
print(self.entry.get())
self.button.config(text='Button Clicked.')
mw = Tk()
main = Main(mw)
mw.mainloop()
I want a button in my window to open a new window and close the previous one. Is it possible to have one button do both of these? I've tried in the following code, but it hasn't worked, just told me that window is not defined:
import tkinter
def window1():
window = tkinter.Tk()
tkinter.Button(window, text = "Next", command = window2).pack()
window.mainloop()
def window2():
window.destroy() #This is where the error is
menu = tkinter.Tk()
etc, etc, etc
window1()
First, you need to return the window object from the first function:
def window1():
window = tkinter.Tk()
tkinter.Button(window, text = "Next", command = lambda: window2(window)).pack()
window.mainloop()
return window
Then, you need to pass the window as an argument to your function:
def window2(window):
window.destroy()
menu = tkinter.Tk()
And then call window1 with:
window = window1()
and click the button to destroy it and do the rest
This is an example using Toplevels, which is usually a better choice than creating, destroying, re-creating Tk() instances. The unique Toplevel ID is passed to the close_it function using partial(). You would, of course, combine them or have the close function call the open function.
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
class OpenToplevels():
""" open and close additional Toplevels with a button
"""
def __init__(self):
self.root = tk.Tk()
self.button_ctr=0
but=tk.Button(self.root, text="Open a Toplevel",
command=self.open_another)
but.grid(row=0, column=0)
tk.Button(self.root, text="Exit Tkinter", bg="red",
command=self.root.quit).grid(row=1, column=0, sticky="we")
self.root.mainloop()
def close_it(self, id):
id.destroy()
def open_another(self):
self.button_ctr += 1
id = tk.Toplevel(self.root)
id.title("Toplevel #%d" % (self.button_ctr))
tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
command=partial(self.close_it, id),
bg="orange", width=20).grid(row=1, column=0)
Ot=OpenToplevels()
Yes. Is possible. But you'll need to def that:
def window1:
blablabla
blablabla
def window2:
window2.destroy() <-- Here where the error was
How you noticed, put your name of window what you want Destroy and it will work!
using Python3
You could use a "global" such as:
root = Tk()
root.title('This is the root window')
def window_create():
global window_one
window_one = Tk()
window_one.title('This is window 1')
Then, from any function (or elsewhere) when you want to destroy window_one, do:
def window_destroyer():
window_one.destroy()
You could call your window_destroyer function from a button anywhere such as root which the example shows:
kill_window_btn = Button(root, text="Destroy", command=window_destroyer).pack()
Of course, follow your own naming conventions. :)
It seems to me, just 'global window_one' would solve it.
I've been trying to develop a small gui to calculate some totals with weighting etc. While trying to get a total to update real-time with a changing entry, I noticed it took two events to update; This is a simplified version of my code that shows the problem:
from Tkinter import *
root=Tk()
frame=Frame(root)
frame.pack()
entry=Entry(frame)
entry.pack()
label=Label(frame,text="entry:")
label.pack()
def updatelabel(event):
label=Label(frame,text="entry:"+entry.get())
label.pack()
print "called"
entry.bind("<Key>", updatelabel)
root.mainloop()
When you input into the field the function calls, but does not update to what was typed until the next character is typed. How would I go about getting the label to update to what is in the field at the time?
You don't really need to explicitly process events and use callback functions to accomplish what you want. In other words it's possible to get Tkinter to do it for you automatically using a StringVar().
from Tkinter import *
root=Tk()
frame=Frame(root)
frame.pack()
entry_var = StringVar()
entry_var.set('')
entry = Entry(frame, textvariable=entry_var)
entry.pack()
label = Label(frame, text='entry: ')
label.pack(side=LEFT)
contents = Label(frame, textvariable=entry_var)
contents.pack(side=LEFT)
entry.focus_set() # force initial keyboard focus to be on entry widget
root.mainloop()
Instead of using entry.bind("<Key>", update label), I used the root window instead: root.bind("<Key>", update label). This did the trick, however it is important to realize that the function updatelabel() will be called every time a key is pressed in your tkinter window. This could cause some problems if you have more than one entry box updating labels.
Here is the code I wrote with a few modifications:
from Tkinter import *
root=Tk()
frame=Frame(root)
frame.pack()
update_label = StringVar() # Made a StringVar so you don't get new labels every time a key is pressed.
update_label.set("entry:")
entry=Entry(frame)
entry.pack()
label=Label(frame,textvariable=update_label) # Used textvariable= instead of text=
label.pack()
def updatelabel(event):
update_label.set("entry:" + entry.get()) # Setting the variable used in textvariable=
print "called"
root.bind("<Key>", updatelabel) # Changed entry.bind to root.bind
root.mainloop()
No it doesn't require two entries to be called, it is called on the first entry. The key bindings are on the Entry widgets to avoid the problems that a binding to the root will create if you have more than one entry widget.
import tkinter as tk
class SmallApp(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self, master)
self.master = master
self.pack()
self.entry = tk.Entry(self)
self.entry.pack()
self.var = "entry:"
self.label = tk.Label(text = self.var)
self.label.pack()
self.entry.bind("<Key>", self.updatelabel)
def updatelabel(self, event):
self.var += event.char
self.label.configure(text=self.var)
root = tk.Tk()
app = SmallApp(root)
app.mainloop()