There are two methods for popup windows in Python: Tkinter: Use the Tkinter library to create Tk or TopLevel widgets. Pyglet: Use the Pyglet library to create Window windows.
How to pop up a window in Python
In Python, there are two main ways to pop up a window:
1. Tkinter
Tkinter is Python’s standard GUI library, which provides Tk
and TopLevel
widgets , used to create and manage windows.
<code>import tkinter as tk # 创建一个主窗口 root = tk.Tk() # 创建一个弹出窗口 popup = tk.Toplevel(root) popup.title("弹出窗口") popup.geometry("300x200") popup.configure(bg="white") # 在弹出窗口中添加一个标签 label = tk.Label(popup, text="这是一个弹出窗口") label.pack() # 显示主窗口和弹出窗口 root.mainloop()</code>
2. Pyglet
Pyglet is a cross-platform window system and multimedia library. It provides the Window
class for creating and managing windows.
<code>import pyglet # 创建一个窗口 window = pyglet.window.Window(width=300, height=200, caption="弹出窗口") # 在窗口中添加一个标签 label = pyglet.text.Label("这是一个弹出窗口", font_name="Arial", font_size=12, x=window.width // 2, y=window.height // 2, anchor_x="center", anchor_y="center") @window.event def on_draw(): window.clear() label.draw() # 运行窗口 pyglet.app.run()</code>
Which method to choose
Both Tkinter and Pyglet can be used for popups, but they each have their own pros and cons:
The above is the detailed content of How to pop up a window in python. For more information, please follow other related articles on the PHP Chinese website!