Is there a way to get the messagebox to appear in front of the toplevel?
code:
showwarning('warning','input four parameters!') shows the messagebox in front of mainwindow not toplevel window.
I checked the similar question root = Tk() texto = Toplevel(root) showinfo("title", "message") and the solution is ,showinfo("title", "message",parent=texto)
while I use class,there is no parent defination.
from tkinter import *
from tkinter.ttk import *
from tkinter.messagebox import *
import os
currentpath=os.path.dirname(__file__)
class AcurateQueryWindow(Toplevel):
def __init__(self,all_node_list:list):
super().__init__()
self.title("AcurateQuery")
self.width = 600
self.heigh = 500
self.screenwidth = self.winfo_screenwidth()
self.screenheight = self.winfo_screenheight()
self.geometry('%dx%d+%d+%d'%(self.width, self.heigh, (self.screenwidth-self.width)/2, (self.screenheight-self.heigh)/2))
self.resizable(0,0)
self.focus_set()
self.all_node_list = all_node_list
self.acurate_query_node_list=[]
self.setup_UI()
def setup_UI(self):
self.Style01 = Style()
self.Style01.configure("title.TLabel", font=("Helvetica", 25, "bold"), foreground="navy")
self.Style01.configure("pass.TLabel", font=("Helvetica", 15, "bold"), foreground="navy")
self.Style01.configure("TButton", font=("Helvetica", 12, "bold"))
self.Login_image = PhotoImage(file=currentpath+ os.sep + "img" + os.sep + "stu_detail_banner.png")
self.Label_image = Label(self, image=self.Login_image)
self.Label_image.pack()
# title
self.Label_title = Label(self, text="==AcurateQuery==", style="title.TLabel")
self.Label_title.place(x=280, y=15)
# pane
self.Pane_detail = PanedWindow(self, width=590, height=380)
self.Pane_detail.place(x=5, y=88)
#
self.Label_rlinemin = Label(self.Pane_detail,text = "rlinemin:",style = "pass.TLabel")
self.Label_rlinemin.place(x=160,y=70)
self.var_rlinemin = StringVar()
self.Entry_rlinemin = Entry(self.Pane_detail,textvariable = self.var_rlinemin , font=("Helvetica", 15, "bold"),width = 12)
self.Entry_rlinemin.place(x=280,y=68)
#
self.Label_rlinemax = Label(self.Pane_detail, text="rlinemax:",style = "pass.TLabel")
self.Label_rlinemax.place(x=160, y=120)
self.var_rlinemax = StringVar()
self.Entry_rlinemax = Entry(self.Pane_detail,textvariable=self.var_rlinemax, font=("Helvetica", 15, "bold"), width=12)
self.Entry_rlinemax.place(x=280, y=118)
#
self.Label_rpointmin = Label(self.Pane_detail, text="rpointmin:",style = "pass.TLabel")
self.Label_rpointmin.place(x=160, y=170)
self.var_rpointmin = StringVar()
self.Entry_rpointmin = Entry(self.Pane_detail,textvariable=self.var_rpointmin, font=("Helvetica", 15, "bold"), width=12)
self.Entry_rpointmin.place(x=280, y=168)
#
self.Label_rpointmax = Label(self.Pane_detail, text="rpointmax:",style = "pass.TLabel")
self.Label_rpointmax.place(x=160, y=220)
self.var_rpointmax = StringVar()
self.Entry_rpointmax = Entry(self.Pane_detail,textvariable=self.var_rpointmax, font=("Helvetica", 15, "bold"), width=12)
self.Entry_rpointmax.place(x=280, y=218)
#
self.Button_save = Button(self, text="query", style="TButton",command = self.query)
self.Button_save.place(x=300, y=452)
self.Button_exit = Button(self, text="close", style="TButton",command = self.close_window)
self.Button_exit.place(x=450, y=452)
def close_window(self):
self.destroy()
def query(self):
if len(self.Entry_rlinemin.get())==0 or len(self.Entry_rlinemax.get())==0 or len(self.Entry_rpointmin.get())==0 or len(self.Entry_rpointmax.get())==0:
showwarning('warning','input four parameters!')
pass
else:
rlinemin=int(str(self.Entry_rlinemin.get()).strip())
rlinemax=int(str(self.Entry_rlinemax.get()).strip())
rpointmin=int(str(self.Entry_rpointmin.get()).strip())
rpointmax=int(str(self.Entry_rpointmax.get()).strip())
if rlinemin>rlinemax or rpointmin>rpointmax:
showwarning('warning','max must bigger than min')
else:
self.acurate_query_node_list.append([rlinemin,rlinemax,rpointmin,rpointmax])
self.destroy()
return self.acurate_query_node_list
Is there a way to get the messagebox to appear in front of the toplevel while I use class?
I am trying to do a message box to warns the user in my UI that they will be moved to the homepage after 10 seconds, codes are as following:
from tkinter import *
import warnings
import random
import time
import sqlite3
from tkinter import simpledialog
from tkinter import messagebox
from tkcalendar import *
from tkinter import ttk
import math
from PIL import Image, ImageTk
import winsound
##-------------Frames setup--------------------------
class VendingApp(Tk):
def __init__(self):
Tk.__init__(self)
self._frame = None
self.switch_frame(Home)
def switch_frame(self, frame_class):
#Destroys current frame and replaces it with a new one.
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
####-----------------------Home page---------------------------
class Home(Frame):
def __init__(self, master):
Frame.__init__(self, master)
##-----------------------fuctions-----------------------------------
def clicked(a):
if (a.x <=1920) and (a.y<=1080):
master.switch_frame(Store)
print ("1")
else:
None
None
##----------------setup------------------------------------------
self._images = list()
img_banner = Image.open("pic/banner.jpg")
img_banner = img_banner.resize((400,100), Image.ANTIALIAS)
banner = ImageTk.PhotoImage(img_banner)
self._images.append(banner)
##---------------------Top frame Home------------------------------------
topFrame = Frame(self,width = 1920, height = 1080)
topFrame.pack()
canvasM = Canvas(topFrame,height=1080, width=1920)
canvasM['highlightthickness'] = 0
canvasM.pack()
body = canvasM.create_rectangle(0, 0,1920,1080, fill = 'gray95')
mylabel = canvasM.create_text((960, 390),font=("Purisa", 40), text="Touch anywhere to continue")
canvasM.tag_bind(body,"<Button>",clicked)
canvasM.tag_bind(mylabel,"<Button>",clicked)
#-------------------------Store page--------------------------------------------------------------
class Store(Frame):
def __init__(self, master):
Frame.__init__(self, master)
def timeset():
global time
time = 15
def active(event):
global time
time = 15
print (time)
def timeout():
global time
if time >= 10:
time -= 1
master.after(1000, timeout)
elif time <= 10 and time > 0:
askuser()
time -= 1
master.after(1000, timeout)
elif time <= 0:
master.switch_frame(Home)
print(time)
def askuser():
ask = messagebox.showinfo('Are you there?','Returning to home page in ' + (str(time)) + ' seconds. \n please touch the screen to continue using the app')
self._images = list()
img_banner = Image.open("pic/banner.jpg")
img_banner = img_banner.resize((400, 100), Image.ANTIALIAS)
banner = ImageTk.PhotoImage(img_banner)
self._images.append(banner)
##---------------------pictures and filters Store------------------------------------
PicFrame = Frame(self, width=400, height=100)
PicFrame.grid(row = 0, column = 0)
PicFrame.grid_propagate(False)
canvas_for_banner = Canvas(PicFrame, height=100, width=400 ) # banner image
canvas_for_banner.pack(anchor = N)
canvas_for_banner['highlightthickness'] = 0
canvas_for_banner.grid_propagate(False)
canvas_for_banner.create_image(0, 0, anchor=NW, image=banner)
##---------------------------------Midd store-------------------
FilterFrame =Frame(self, width=400, height=930, relief = RAISED, bd =1)
FilterFrame.grid(row = 1, column = 0)
FilterFrame.pack_propagate(0)
Ava_title = Label(FilterFrame, text = "Availability", font = ('Helvetica', 20, 'bold'))
Ava_title.pack(pady = (100,0))
self.stock_yes = IntVar()
self.stock_no = IntVar()
check_ava = Checkbutton(FilterFrame, text = 'Avaliable', variable = self.stock_yes, font = 20, command = None)
check_ava.pack( anchor = 'w', padx = 30, pady =10)
check_unava = Checkbutton(FilterFrame, text = 'Unavaliable', font = 20, variable = self.stock_no, command = None)
check_unava.pack(anchor = 'w', padx = 30, pady = 10)
gend_title = Label(FilterFrame, text = "Gender", font = ('Helvetica', 20, 'bold'))
gend_title.pack(pady = (30,0))
self.boi = IntVar()
self.girl = IntVar()
check_boi = Checkbutton(FilterFrame, text = 'Male', font = 20, variable = self.boi)
check_boi.pack( anchor = 'w', padx = 30, pady = 10)
cehck_girl = Checkbutton(FilterFrame, text = 'Female', font = 20, variable = self.girl)
cehck_girl.pack(anchor = 'w', padx = 30, pady =10)
Class_title = Label(FilterFrame, text = "Uniform class", font = ('Helvetica', 20, 'bold'))
Class_title.pack(pady = (30,0))
self.formal = IntVar()
self.sport = IntVar()
check_formal = Checkbutton(FilterFrame, text = 'Formal', font = 20, variable = self.formal)
check_formal.pack( anchor = 'w', padx = 30, pady =10)
check_sport = Checkbutton(FilterFrame, text = 'Sport', font = 20, variable = self.sport)
check_sport.pack(anchor = 'w', padx = 30, pady =10)
type_title = Label(FilterFrame, text = "Type", font = ('Helvetica', 20, 'bold'))
type_title.pack(pady = (30,0))
self.shirts = IntVar()
self.pants = IntVar()
self.misc = IntVar()
check_shirts = Checkbutton(FilterFrame, text = 'Shirts', font = 20, variable = self.shirts)
check_shirts.pack( anchor = 'w', padx = 30, pady =10)
check_pants = Checkbutton(FilterFrame, text = 'Pants', font = 20, variable = self.pants)
check_pants.pack(anchor = 'w', padx = 30, pady =10)
check_misc = Checkbutton(FilterFrame, text = 'Misc', font = 20, variable = self.misc)
check_misc.pack(anchor = 'w', padx = 30, pady =10)
##------------------------\\\\\\\\\\\\\\\\\\\\\\---------------------------------------------
MidFrame = Frame(self,width = 1520, height = 1030, relief = SUNKEN, bd = 2)
MidFrame.grid(row = 0, column = 1, rowspan = 2)
MidFrame.grid_propagate(False)
store_canvas = Canvas(MidFrame, width = 1520, height = 1030)
store_canvas.pack()
store_canvas.pack_propagate(0)
frames = []
frame_order = []
num = 1
for x in range(4):
frames.append([])
for y in range(4):
frames[x].append(0)
for x in range(4):
for y in range(4):
frames[x][y] = Frame(store_canvas, width=1520 / 4, height=1030 / 4, bd = 2, relief = SOLID)
frames[x][y].grid(row=y, column=x)
frames[x][y].pack_propagate(False)
frame_order.append(frames[x][y])
for frame in frame_order:
Label(frame, text=num, anchor='nw').pack( side = 'left')
num += 1
##------------------------\\\\\\\\\\\\\\\\\\\\\\---------------------------------------------
BottomFrame = Frame(self,width = 1920, height = 50, bd = 2, relief = RAISED)
BottomFrame.grid(row = 2, column =0, columnspan = 2)
BottomFrame.pack_propagate(False)
help_btn = Button(BottomFrame, width = 5, height = 3, text = '?', image = None)
help_btn.pack(side = 'right')
master.bind("<Button>",active)
timeset()
timeout()
#----------------------------------------------------------------------------------------
if __name__ == "__main__":
root = VendingApp()
#Renames the TITLE of the window
root.title("Vending machine")
root.geometry("1920x1080")
root.attributes('-fullscreen', True)
root.resizable(False, False)
root.mainloop()
The problem is that the whole program 'freezes' every time the msg box pop up and unless the user confirms 'ok' then the function will pick up where it left off. Is there any way to keep the function going, hence the number in the msg box will update according to the time remaining? Am I approaching this problem the wrong way? Is there another module for this all along and I am just using the wrong module for the task? Please go easy on me, I am still learning. All responses all much appreciated.
messagebox will pause the further execution until it receives an input. To prevent this you can try the following
Use threading
from tkinter import *
from tkinter import messagebox
from threading import Thread
def msgbox():
def _display():
messagebox.showinfo('Info','Self distruction after 2 seconds')
Thread(target=_display).start()
root.after(2000,root.destroy)
root=Tk()
button=Button(root,text='Run',command=msgbox)
button.pack()
root.mainloop()
Create your own info box using Toplevel (the below example will create a replica (sort of) of standard windows info box)
from tkinter import *
import tkinter.ttk as ttk
class InfoBox(Toplevel):
def __init__(self,title,message,parent=None):
Toplevel.__init__(self,parent)
self.bell()
self.transient(self.master)
self.title(title)
self.config(bg='white')
top_frame=Frame(self,bd=0,bg=self['bg'])
top_frame.pack(side='top',fill='x',pady=20)
bottom_frame=Frame(self,bd=0)
bottom_frame.pack(side='bottom',fill='x')
self.info_icon=Canvas(top_frame,width=36,height=36
,bg=self['bg'],bd=0,highlightthickness=0)
self.info_icon.create_oval(0,0,30,30,fill='#0077be',outline='#0077be')
self.info_icon.create_text(15,16,text='i',font=('',18),fill='white')
self.info_icon.pack(side='left',padx=(20,0),anchor='center')
self.label=Label(top_frame,text=message,bg=self['bg'])
self.label.pack(padx=(1,20),pady=5,anchor='center')
self.ok_button=ttk.Button(bottom_frame,text='OK',
state='active',command=self.destroy)
self.ok_button.pack(anchor='e',padx=15,pady=10)
self.update()
center_x=self.winfo_screenwidth()//2-self.winfo_width()//2
center_y=self.winfo_screenheight()//2-self.winfo_height()//2
self.geometry(f'+{center_x}+{center_y}')
def msgbox():
InfoBox('Info','Self distruction after 2 seconds')
root.after(2000,root.destroy)
root=Tk()
button=Button(root,text='Run',command=msgbox)
button.pack()
root.mainloop()
UPDATE
Stacking is automatically prevented
from tkinter import *
import tkinter.ttk as ttk
class InfoBox(Toplevel):
def __init__(self):
self.exists=False
def call(self,title,message,parent=None):
if self.exists:
self._destroy()
Toplevel.__init__(self,parent)
self.bell()
self.transient(self.master)
self.title(title)
self.config(bg='white')
top_frame=Frame(self,bd=0,bg=self['bg'])
top_frame.pack(side='top',fill='x',pady=20)
bottom_frame=Frame(self,bd=0)
bottom_frame.pack(side='bottom',fill='x')
self.info_icon=Canvas(top_frame,width=36,height=36
,bg=self['bg'],bd=0,highlightthickness=0)
self.info_icon.create_oval(0,0,30,30,fill='#0077be',outline='#0077be')
self.info_icon.create_text(15,16,text='i',font=('',18),fill='white')
self.info_icon.pack(side='left',padx=(20,0),anchor='center')
self.label=Label(top_frame,text=message,bg=self['bg'])
self.label.pack(padx=(1,20),pady=5,anchor='center')
self.ok_button=ttk.Button(bottom_frame,text='OK',
state='active',command=self._destroy)
self.ok_button.pack(anchor='e',padx=15,pady=10)
self.update()
center_x=self.winfo_screenwidth()//2-self.winfo_width()//2
center_y=self.winfo_screenheight()//2-self.winfo_height()//2
self.geometry(f'+{center_x}+{center_y}')
self.protocol('WM_DELETE_WINDOW',self._destroy)
self.exists=True
def _destroy(self):
self.destroy()
self.exists=False
root=Tk()
infobox=InfoBox()
button=Button(root,text='Hello',command=lambda:infobox.call('Info','Hello'))
button.pack()
button1=Button(root,text='World',command=lambda:infobox.call('Info','World'))
button1.pack()
root.mainloop()
I can't seem to make my new window display the inputer code
it only displays a new empty window without the labels and the input widget
i want to make a simple GUI for a school project, but i dont want to type the name in the python shell, but in the interface
please help and tell me where im wrong, im still new to python
from collections import deque
first = deque([])
last = deque([])
nom = 0
import tkinter as tk
from tkinter import *
class Application(tk.Frame):
def __init__(self, master=None, nom=0, priono=1, prio=1):
super().__init__(master)
self.pack()
self.create_widgets()
self.nom= nom
self.priono= priono
self.prio=prio
def create_widgets(self):
self.add = tk.Button(self, bg ="black", fg="pink")
self.add["text"] = "-----Add to queue-----\n(click me)"
self.add["command"] = self.create_window
self.add.pack(side="top", pady = 10, padx = 20)
self.add["font"] = "Times 16 bold"
self.remov = tk.Button(self, bg ="black", fg="pink")
self.remov["text"]= "---Remove in queue---\n(click me)"
self.remov["command"] = self.remov_
self.remov.pack(side="top", pady = 10, padx = 20)
self.remov["font"] = "Times 16 bold"
self.quit = tk.Button(self, text="QUIT", fg="white", bg = "red",
command=root.destroy)
self.quit.pack(side="bottom")
def create_window(self):
def inputer(self):
self.L1 = tk.Label(self, text = "Name")
self.L1.pack( side = LEFT)
self.E1 = tk.Entry(self, bd =5)
self.E1.pack(side = RIGHT)
win2 = Toplevel(self)
win2.button = Button(self, text='Ready?\nClick Here', command=inputer)
def say_hi(self):
print("hi there, everyone!")
def add_1(self):
name=input("What is your name? ")
first.append(name)
print("Good Day!",first[self.nom])
print("Your priority number is:","%03d" % (self.priono))
self.priono+=1
self.nom+= 1
def remov_(self):
if (len(first)) >= 1:
first.popleft()
else:
print("No one left in queue")
root = tk.Tk()
root.config(background="black")
app = Application(master=root)
app.master.title("ID QUEUEING SYSTEM beta")
app.master.minsize(540, 360)
app.master.maxsize(600, 500)
app.mainloop()
You must use your toplevel to indicate where the widgets must appear; you must also pack (place or grid) the widgets belonging to your toplevel window.
The code from inputer needed to be placed outside of the inner function; you can now write the code to have this inputer do what you need it to do:
I removed the star import and added the prefix tk. to all tkinter method calls.
import tkinter as tk
from collections import deque
class Application(tk.Frame):
def __init__(self, master=None, nom=0, priono=1, prio=1):
super().__init__(master)
self.pack()
self.create_widgets()
self.nom = nom
self.priono= priono
self.prio=prio
def create_widgets(self):
self.add = tk.Button(self, bg ="black", fg="pink")
self.add["text"] = "-----Add to queue-----\n(click me)"
self.add["command"] = self.create_window
self.add.pack(side="top", pady = 10, padx = 20)
self.add["font"] = "Times 16 bold"
self.remov = tk.Button(self, bg ="black", fg="pink")
self.remov["text"]= "---Remove in queue---\n(click me)"
self.remov["command"] = self.remov_
self.remov.pack(side="top", pady = 10, padx = 20)
self.remov["font"] = "Times 16 bold"
self.quit = tk.Button(self, text="QUIT", fg="white", bg = "red",
command=root.destroy)
self.quit.pack(side="bottom")
def create_window(self):
def inputer():
print('inputer ', end=': ')
print(self.E1.get())
win2 = tk.Toplevel(self)
win2_button = tk.Button(win2, text='Ready?\nClick Here', command=inputer)
win2_button.pack()
self.L1 = tk.Label(win2, text = "Name")
self.L1.pack( side=tk.LEFT)
self.E1 = tk.Entry(win2, bd =5)
self.E1.pack(side=tk.RIGHT)
def say_hi(self):
print("hi there, everyone!")
def add_1(self):
name=input("What is your name? ")
first.append(name)
print("Good Day!",first[self.nom])
print("Your priority number is:","%03d" % (self.priono))
self.priono+=1
self.nom+= 1
def remov_(self):
if (len(first)) >= 1:
first.popleft()
else:
print("No one left in queue")
root = tk.Tk()
root.config(background="black")
app = Application(master=root)
app.master.title("ID QUEUEING SYSTEM beta")
app.master.minsize(540, 360)
app.master.maxsize(600, 500)
app.mainloop()
Sorry for the mess
so I am making a seating chart, and I cant seem to get it working properly... again. I am trying to make the label reset every time i press the run button, any ideas?
#commands: add name , Run
#imports
import random
from time import sleep
from tkinter import *
#Console and background Handlers
Tables = 6
Names = []
def AddNames():
NewNames = e.get("1.0", 'end -1c')
if NewNames in Names:
print("Name Already exists")
elif NewNames == "":
print("Empty")
else:
Names.append(NewNames)
print(Names)
e.delete(1.0, END)
def Random():
RandomNum = random.randrange(Tables)
if RandomNum == 0:
RandomNum = random.randrange(Tables)
return RandomNum
def run():
X = 0
for i in Names:
#print(Names[X])
print("Table: " + str(Random()))
X += 1
#text = Label(popup, text = "")
text = Label(popup, text= Names[X] + "\n" + "Table: " + str(Random()))
text.pack()
#GUI Handler
root = Tk()
root.geometry("1024x768")
e = Text(root, bd = 10, font=("Comic Sans MS", 50) ,width = 15, height = 2)
e.pack()
popup = Toplevel()
popup.title("Seating Chart")
AddNameButton = Button(root, text = ("Add Name"), width = 15, height = 5, command = AddNames)
AddNameButton.pack()
RunButton = Button(root, text = ("Run"), width = 15, height = 5, command = run)
RunButton.pack()
root.mainloop()
I am trying to reset text every time the user presses the run button
import tkinter
from tkinter import ttk
import random
class MyApp:
def __init__(self):
self.root = tkinter.Tk()
self.seatwindow = None
self.root.title('Add Names')
self.currentname = tkinter.StringVar()
self._maxtables = tkinter.StringVar()
self.addednames = []
self.commandframe = ttk.Labelframe(self.root, text='Commands')
self.nameentry = ttk.Entry(self.root, textvariable=self.currentname)
self.addbutton = ttk.Button(self.root, text='Add Name', command=self.addname)
self.maxtablabel = ttk.Label(self.root, text='Tables: ')
self.maxtabentry = ttk.Entry(self.root, textvariable=self._maxtables)
self.genbutton = ttk.Button(self.commandframe, text='Run', command=self.generate)
self.resetbutton = ttk.Button(self.commandframe, text='Reset', command=self.reset)
self._maxtables.set('6')
self.nameentry.grid(row=0, column=0)
self.addbutton.grid(row=0, column=1, sticky='nsew')
self.maxtabentry.grid(row=1, column=1, sticky='nsw')
self.maxtablabel.grid(row=1, column=0, sticky='nse')
self.genbutton.grid(row=0, column=0, sticky='nsew')
self.resetbutton.grid(row=0, column=1, sticky='nsew')
self.commandframe.grid(row=2, column=0, columnspan=2, sticky='nsew')
self.nameentry.bind('<Return>', self.addname)
self.root.bind('<Control-Return>', self.generate)
def addname(self, event=None):
name = self.currentname.get()
if not(name == '' or name in self.addednames):
self.addednames.append(name)
self.currentname.set('')
else:
self.currentname.set('Name already added!')
def generate(self, event=None):
if not self.seatwindow == None:
self.seatwindow.destroy()
self.currentname.set('')
self.seatwindow = tkinter.Toplevel()
random.shuffle(self.addednames)
tables = []
for i in range(self.maxtables):
tableframe = ttk.Labelframe(self.seatwindow, text='Table ' + str(i + 1) + ':')
tableframe.grid(column=i, row=0, sticky='nsew')
tables.append(tableframe)
for index, name in enumerate(self.addednames):
namelabel = ttk.Label(tables[index%self.maxtables], text=name)
namelabel.grid(column=0, row=index//self.maxtables + 1)
def reset(self):
self.currentname.set('')
self.maxtables = 6
self.addednames = []
def run(self):
self.root.mainloop()
#property
def maxtables(self):
return int(self._maxtables.get())
MyApp().run()