📜  改变MessageBox的位置——Tkinter

📅  最后修改于: 2022-05-13 01:54:58.200000             🧑  作者: Mango

改变MessageBox的位置——Tkinter

先决条件:

  • Python tkinter
  • Python tkinter 消息框小部件
  • Python tkinter 顶级小部件

Python提供了许多用于创建和开发 GUI(图形用户界面)的库和框架。在所有 GUI 库或框架中,Tkinter 是最常用的方法,因为它在创建 GUI 应用程序时相对快速和容易。

创建消息框

MessageBox Widget 用于在Python应用程序中显示消息框。要创建消息框,请使用Toplevel()小部件。 Toplevel Widget 用于在所有其他窗口之上创建一个窗口。它用于向用户提供一些额外的信息。这些窗口由窗口管理器直接组织和管理,不需要每次都关联任何父窗口。

一旦您的消息框准备就绪,就可以向其中添加标签和按钮以提供特定信息。

句法:

Toplevel widget
toplevel = Toplevel(root, bg, fg, bd, height,....)

Create Label
Label(toplevel,text="Enter text here..").pack()

Create Button
Button(toplevel,text="Enter text here..").pack()

Set Size
toplevel=Toplevel(root)
toplevel.geometry("dimension")

例子:

Python3
from tkinter import *
 
def messagebox():
    toplevel = Toplevel(root)
 
    toplevel.title("QUIT")
    toplevel.geometry("300x100")
 
 
    l1=Label(toplevel, image="::tk::icons::question")
    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
    l2=Label(toplevel,text="Are you sure you want to Quit")
    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
 
    b1=Button(toplevel,text="Yes",command=root.destroy,width = 10)
    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
    b2=Button(toplevel,text="No",command=toplevel.destroy,width = 10)
    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
 
 
root = Tk()
root.geometry("300x200")
root.title("Main Window")
Button(root,text="Quit",command=messagebox,width = 7).pack(pady=80)
 
root.mainloop()


Python3
from tkinter import *
 
def messagebox():
    toplevel = Toplevel(root)
 
    toplevel.title("QUIT")
    toplevel.geometry(f"300x100+{root.winfo_x()}+{root.winfo_y()}")
     
 
    l1=Label(toplevel, image="::tk::icons::question")
    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
    l2=Label(toplevel,text="Are you sure you want to Quit")
    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
 
    b1=Button(toplevel,text="Yes",command=root.destroy,width = 10)
    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
    b2=Button(toplevel,text="No",command=toplevel.destroy,width = 10)
    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
 
 
root = Tk()
root.geometry("300x200")
root.title("Main Window")
Button(root,text="Quit",command=messagebox,width = 7).pack(pady=80)
 
root.mainloop()


Python3
from tkinter import *
 
 
def messagebox():
    toplevel = Toplevel(root)
 
    toplevel.title("QUIT")
    x_position = 300
    y_position = 200
    toplevel.geometry(f"300x100+{x_position}+{y_position}")
 
    l1=Label(toplevel, image="::tk::icons::question")
    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
    l2=Label(toplevel, text="Are you sure you want to Quit")
    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
 
    b1=Button(toplevel, text="Yes", command=root.destroy, width=10)
    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
    b2=Button(toplevel, text="No", command=toplevel.destroy, width=10)
    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
 
 
root = Tk()
root.geometry("300x200")
root.title("Main Window")
Button(root, text="Quit", command=messagebox, width=7).pack(pady=80)
 
root.mainloop()


输出:

主窗口(带按钮)

消息框(带标签和按钮)

设置位置

到目前为止,tkinter 中没有内置方法来设置消息框的位置。但位置可以明确处理。

句法:

toplevel.geometry(f"dimension+{root.winfo_x()}+{root.winfo_y()}")
  • root.winfo_x()- 给出主窗口的 X 当前位置
  • root.winfo_y()- 给出主窗口的 Y 当前位置

或者

x_position = value
y_position = value

toplevel.geometry(f"dimension+{x_position}+{y_position}")

示例 1:

蟒蛇3

from tkinter import *
 
def messagebox():
    toplevel = Toplevel(root)
 
    toplevel.title("QUIT")
    toplevel.geometry(f"300x100+{root.winfo_x()}+{root.winfo_y()}")
     
 
    l1=Label(toplevel, image="::tk::icons::question")
    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
    l2=Label(toplevel,text="Are you sure you want to Quit")
    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
 
    b1=Button(toplevel,text="Yes",command=root.destroy,width = 10)
    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
    b2=Button(toplevel,text="No",command=toplevel.destroy,width = 10)
    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
 
 
root = Tk()
root.geometry("300x200")
root.title("Main Window")
Button(root,text="Quit",command=messagebox,width = 7).pack(pady=80)
 
root.mainloop()

输出:

主窗口

当您单击“退出”按钮时

留言箱

示例 2:

蟒蛇3

from tkinter import *
 
 
def messagebox():
    toplevel = Toplevel(root)
 
    toplevel.title("QUIT")
    x_position = 300
    y_position = 200
    toplevel.geometry(f"300x100+{x_position}+{y_position}")
 
    l1=Label(toplevel, image="::tk::icons::question")
    l1.grid(row=0, column=0, pady=(7, 0), padx=(10, 30), sticky="e")
    l2=Label(toplevel, text="Are you sure you want to Quit")
    l2.grid(row=0, column=1, columnspan=3, pady=(7, 10), sticky="w")
 
    b1=Button(toplevel, text="Yes", command=root.destroy, width=10)
    b1.grid(row=1, column=1, padx=(2, 35), sticky="e")
    b2=Button(toplevel, text="No", command=toplevel.destroy, width=10)
    b2.grid(row=1, column=2, padx=(2, 35), sticky="e")
 
 
root = Tk()
root.geometry("300x200")
root.title("Main Window")
Button(root, text="Quit", command=messagebox, width=7).pack(pady=80)
 
root.mainloop()

输出:

当您单击“退出”按钮时,它会在位置 (300,200) 显示消息框