I want to use the listbox in tkinter in order to open a new window with a button, but when I'm pressing a message it only opens the window, without the button.
from tkinter import *
from playsound import playsound
import os
# create a window as root using Tk() function
root = Tk()
root.geometry("200x200")
# create a changecolor function
# to change background color of window
def new_win():
top2 = Toplevel(root)
top2.geometry('200x200')
top2.title("display Window")
# photo2 = PhotoImage(file=r"hearing.png")
button = Button(text='wtf')
def changecolor(event):
# get selected list box color item
new_win()
# create listbox
listbox = Listbox(root , font=('times 20 bold'), height=5,width=10)
# insert color items into listbox
listbox.insert(1, 'first')
listbox.insert(2, 'second')
listbox.insert(3, 'third')
listbox.insert(4, 'forth')
listbox.pack(pady=20)
# bind the click event with listbox and
# call changecolor() function
listbox.bind('<<ListboxSelect>>', changecolor)
root.mainloop()
Any suggestion would be very helpful.
You can change the line 16
button = Button(text='wtf')
to
button = Button(top2,text='wtf')
button.pack()
Related
Hi I'm new in tkinter and I was trying to code a window with a button creating a new window. I want to add a new button (for cancel) in new window. I cant add a cancel button in my second window.
here is my code:
from tkinter import *
from tkinter.ttk import *
master = Tk()
master.geometry("200x200")
def openNewWindow():
newWindow = Toplevel(master)
newWindow.title("New Window")
newWindow.geometry("200x200")
Label(newWindow,
text="This is a new window").pack()
label = Label(master,
text="This is the main window")
label.pack(pady=10)
btn = Button(master,
text="Click to open a new window",
command=openNewWindow)
btn.pack(pady=10)
mainloop()
Insert the following code lines at the end of openNewWindow function to destroy newWindow.
clo = Button(
newWindow, text = "Close new window", command = newWindow.destroy)
clo.pack(pady = 10)
If you need to perform some action before destroying newWindow then create a function.
def closer():
# do something
newWindow.destroy()
And change openNewWindow close button like this.
clo = Button(
newWindow, text = "Close new window", command = closer)
clo.pack(pady = 10)
I'm trying to create a dropdown menu using the button with images and I will put some functions on each button, but on the drop-down menu that I use the only button that is available to use is a check button. this is the program that I'm currently using.
from tkinter import *
root = Tk()
mbtn = Menubutton(root, text="Options", relief=RAISED)
mbtn.menu = Menu(mbtn, tearoff = 0)
mbtn["menu"] = mbtn.menu
mbtn.menu.add_checkbutton(label="Outing")
mbtn.menu.add_checkbutton(label="Sleep")
mbtn.menu.add_checkbutton(label="Tour")
mbtn.pack()
root.mainloop()
Does this solve Your issue?
from tkinter import *
root = Tk()
mbtn = Menubutton(root, text="Options", relief=RAISED)
mbtn.pack()
mbtn.menu = Menu(mbtn, tearoff = 0)
mbtn["menu"] = mbtn.menu
mbtn.menu.add_checkbutton(label="Outing")
mbtn.menu.add_checkbutton(label="Sleep")
mbtn.menu.add_checkbutton(label="Tour")
root.mainloop()
instead of mbtn.pack() You can obviously also use mbtn.grid() or mbtn.place()(not suggested - .place())
#TheLizzard added: You can use root.config(menu=mbtn.menu) (instead of mbtn.pack()) to make the menu part of the window.
So basically I am making a GUI with Tkinter. I also want to make a frame within the Tkinter window, and it should open when I click a button.
Here is my code so far:
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
button1 = Button(screen)
button.pack()
screen.mainloop
So how do I make a new window(frame) when I click the button?
You can create/toggle frame following the below logic
from tkinter import *
import tkinter
screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")
frame_enabled = False
def toggle_frame():
global frame_enabled
if not frame_enabled:
my_frame.pack(fill='both', expand=True)
else:
my_frame.pack_forget()
frame_enabled = not frame_enabled
button1 = Button(screen, text="Toggle frame", command=toggle_frame)
button1.pack()
my_frame = Frame(screen, bg="red")
screen.mainloop()
I have a list of n elements, I need to create a popup menu for each of them. Each popup would contain some checkboxes.
Condition: A new Toplevel popup window must open after the closure of the previous Toplevel window, and not all at the same time
My code:
from tkinter import *
#root gui
root = Tk()
root.title("test")
root.geometry("300x400")
# Need three popup windows
a = ["one", "two", "three"]
b = []
def open():
for _ in range(len(a)):
top = Toplevel()
top.title("selections")
def next_window():
top.destroy()
show() # This function is supposed to show the selections of each popup window on root gui
for i in range(3):
b.append(IntVar())
b[i].set(0) # de selecting all checkboxes intiially
# checkboxes
Checkbutton(top, text=a[i], variable=b[i]).pack()
Button(top, text = "Submit", command=next_window).pack()
Button(top, text = "skip", command=top.destroy).pack() # this button is used to skip the popup if no selection required
def show():
# printing selections made on each popup window
for i in range(3):
Label(root, text=b[i].get()).pack()
mB = Button(root, text="print selections", command=open).pack()
root.mainloop()
My concern: All three popups are opening at the same time for me now.
You need to call top.wait_window() at the end inside the for loop:
for _ in range(len(a)):
top = Toplevel(root)
top.title("selections")
def next_window():
top.destroy()
show() # This function is supposed to show the selections of each popup window on root gui
for i in range(3):
b.append(IntVar())
b[i].set(0) # de selecting all checkboxes intiially
# checkboxes
Checkbutton(top, text=a[i], variable=b[i]).pack()
Button(top, text = "Submit", command=next_window).pack()
Button(top, text = "skip", command=top.destroy).pack() # this button is used to skip the popup if no selection required
top.grab_set() # route all events to this window
top.wait_window() # wait for current window to close
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()