[英]How to set the position of a MessageBox using tkinter
I have been looking all over to see if I can find any help with this and haven't gotten anywhere My program is a simple tkinter menu that is set to be in a default position in the top left corner of the screen, however when I press the X button it loads the message box in the center of the screen. 我一直到处寻找是否可以找到任何帮助并且没有到达任何地方。我的程序是一个简单的tkinter菜单,设置为屏幕左上角的默认位置,但是当我按下X按钮,它将在屏幕中央加载消息框。
How do I make it so that it snaps the message box to the corner? 我该如何做才能使消息框贴到角落?
root = Tk()
root.geometry('%dx%d+%d+%d' % (300, 224, 0, 0))
root.resizable(0,0)
def exitroot():
if tkMessageBox.askokcancel("Quit", "Are you sure you want to quit?"):
with open(settings, 'wb') as csvfile:
writedata = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writedata.writerow([setpass])
writedata.writerow([opcolour] + [bkcolour])
writedata.writerow([menu_background_status] + [menu_internet_status])
root.destroy()
root.protocol("WM_DELETE_WINDOW", exitroot)`
If any extra code is needed then let me know, and thanks in advance. 如果需要任何额外的代码,请让我知道,并在此先感谢。
You will need to build a custom Toplevel()
window and then tell it to re-position to the corner of the root window. 您将需要构建一个自定义的
Toplevel()
窗口,然后告诉它重新定位到根窗口的角落。 We can do this with the Toplevel()
class and the winfo()
methods. 我们可以使用
Toplevel()
类和winfo()
方法来做到这一点。
import tkinter as tk
# import Tkinter as tk # for Python 2.X
class MessageWindow(tk.Toplevel):
def __init__(self, title, message):
super().__init__()
self.details_expanded = False
self.title(title)
self.geometry("300x75+{}+{}".format(self.master.winfo_x(), self.master.winfo_y()))
self.resizable(False, False)
self.rowconfigure(0, weight=0)
self.rowconfigure(1, weight=1)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
tk.Label(self, text=message).grid(row=0, column=0, columnspan=3, pady=(7, 7), padx=(7, 7), sticky="ew")
tk.Button(self, text="OK", command=self.master.destroy).grid(row=1, column=1, sticky="e")
tk.Button(self, text="Cancel", command=self.destroy).grid(row=1, column=2, padx=(7, 7), sticky="e")
root = tk.Tk()
root.geometry("300x224")
root.resizable(0, 0)
def yes_exit():
print("do other stuff here then root.destroy")
root.destroy()
def exit_root():
MessageWindow("Quit", "Are you sure you want to quit?")
root.protocol("WM_DELETE_WINDOW", exit_root)
root.mainloop()
Results: 结果:
Personally I would build this all in one class inheriting from Tk()
, make the buttons even with ttk buttons and use a label to reference the built in question image located at ::tk::icons::question
like this: 我个人将全部构建在一个继承自
Tk()
,使按钮甚至具有ttk按钮,并使用标签来引用位于::tk::icons::question
的内置问题图像,如下所示:
import tkinter as tk
import tkinter.ttk as ttk
# import Tkinter as tk # for Python 2.X
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.geometry("300x224")
self.resizable(0, 0)
self.protocol("WM_DELETE_WINDOW", self.exit_window)
def yes_exit(self):
print("do other stuff here then self.destroy")
self.destroy()
def exit_window(self):
top = tk.Toplevel(self)
top.details_expanded = False
top.title("Quit")
top.geometry("300x100+{}+{}".format(self.winfo_x(), self.winfo_y()))
top.resizable(False, False)
top.rowconfigure(0, weight=0)
top.rowconfigure(1, weight=1)
top.columnconfigure(0, weight=1)
top.columnconfigure(1, weight=1)
tk.Label(top, image="::tk::icons::question").grid(row=0, column=0, pady=(7, 0), padx=(7, 7), sticky="e")
tk.Label(top, text="Are you sure you want to quit?").grid(row=0, column=1, columnspan=2, pady=(7, 7), sticky="w")
ttk.Button(top, text="OK", command=self.yes_exit).grid(row=1, column=1, sticky="e")
ttk.Button(top, text="Cancel", command=top.destroy).grid(row=1, column=2, padx=(7, 7), sticky="e")
if __name__ == "__main__":
GUI().mainloop()
Results: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.