📜  如何在Python – Tkinter 中按下按钮时创建弹出消息?

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

如何在Python – Tkinter 中按下按钮时创建弹出消息?

在本文中,我们将看到如何在Python中创建按钮以及如何在按下按钮时显示弹出消息。 Tkinter是用于创建 GUI 应用程序的标准Python包。 Tkinter 可能是一组将 Tk 小部件实现为Python类的包装器。 Python与 Tkinter 结合使用时,提供了一种创建 GUI 应用程序的快速而直接的方法。

使用 Tkinter 在Python中创建简单 GUI 的步骤:

  1. 导入 Tkinter 包及其所有模块。
  2. 创建一个 GUI 应用程序根窗口 (root = Tk()),小部件将位于主窗口内。
  3. 使用 mainloop() 调用窗口的无限循环。如果你忘记调用它,用户将不会看到任何东西。该窗口将等待任何用户交互,直到我们关闭它。

示例 1:

Python3
# import everything from tkinter module
from tkinter import *
  
# import messagebox from tkinter module
import tkinter.messagebox
  
# create a tkinter root window
root = tkinter.Tk()
  
# root window title and dimension
root.title("When you press a button the message will pop up")
root.geometry('500x300')
  
# Create a messagebox showinfo
  
def onClick():
    tkinter.messagebox.showinfo("Welcome to GFG.",  "Hi I'm your message")
  
# Create a Button
button = Button(root, text="Click Me", command=onClick, height=5, width=10)
  
# Set the position of button on the top of window.
button.pack(side='bottom')
root.mainloop()


Python3
# import everything from tkinter module
from tkinter import *
  
# import messagebox from tkinter module
import tkinter.messagebox
  
# create a tkinter root window
root = tkinter.Tk()
  
# root window title and dimension
root.title("When you press a any button the message will pop up")
root.geometry('500x300')
  
# Create a messagebox showinfo
  
def East():
    tkinter.messagebox.showinfo("Welcome to GFG", "East Button clicked")
  
def West():
    tkinter.messagebox.showinfo("Welcome to GFG", "West Button clicked")
  
def North():
    tkinter.messagebox.showinfo("Welcome to GFG", "North Button clicked")
  
def South():
    tkinter.messagebox.showinfo("Welcome to GFG", "South Button clicked")
  
# Create a Buttons
  
Button1 = Button(root, text="West", command=West, pady=10)
Button2 = Button(root, text="East", command=East, pady=10)
Button3 = Button(root, text="North", command=North, pady=10)
Button4 = Button(root, text="South", command=South, pady=10)
  
# Set the position of buttons
Button1.pack(side=LEFT)
Button2.pack(side=RIGHT)
Button3.pack(side=TOP)
Button4.pack(side=BOTTOM)
  
root.mainloop()


输出:

点击“点击我”后的输出

示例 2:

蟒蛇3

# import everything from tkinter module
from tkinter import *
  
# import messagebox from tkinter module
import tkinter.messagebox
  
# create a tkinter root window
root = tkinter.Tk()
  
# root window title and dimension
root.title("When you press a any button the message will pop up")
root.geometry('500x300')
  
# Create a messagebox showinfo
  
def East():
    tkinter.messagebox.showinfo("Welcome to GFG", "East Button clicked")
  
def West():
    tkinter.messagebox.showinfo("Welcome to GFG", "West Button clicked")
  
def North():
    tkinter.messagebox.showinfo("Welcome to GFG", "North Button clicked")
  
def South():
    tkinter.messagebox.showinfo("Welcome to GFG", "South Button clicked")
  
# Create a Buttons
  
Button1 = Button(root, text="West", command=West, pady=10)
Button2 = Button(root, text="East", command=East, pady=10)
Button3 = Button(root, text="North", command=North, pady=10)
Button4 = Button(root, text="South", command=South, pady=10)
  
# Set the position of buttons
Button1.pack(side=LEFT)
Button2.pack(side=RIGHT)
Button3.pack(side=TOP)
Button4.pack(side=BOTTOM)
  
root.mainloop()

输出 2:

点击“北”后输出

点击“东”后输出