在 Python-Tkinter 中使用按钮打开一个新窗口
Python提供了多种 GUI(图形用户界面),例如 PyQt、Tkinter、Kivy 等。其中,tkinter 是Python中最常用的 GUI 模块,因为它简单易学,易于实现。 Tkinter 一词来自 tk 接口。 tkinter 模块在Python标准库中可用。
注意:更多信息请参考Python GUI – tkinter
安装
对于Ubuntu ,您必须通过编写以下命令来安装 tkinter 模块:
sudo apt-get install python-tk
当 Tkinter 程序运行时,它会运行一个主循环(一个无限循环),它负责运行一个 GUI 程序。一次只能激活一个 mainloop 实例,因此为了打开一个新窗口,我们必须使用一个小部件Toplevel 。
Toplevel小部件的工作方式与Frame非常相似,但它在单独的顶级窗口中打开,此类窗口具有主窗口(根/主窗口)应具有的所有属性。
要使用按钮打开一个新窗口,我们将使用events 。
示例 1:
Python3
# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
# creates a Tk() object
master = Tk()
# sets the geometry of main
# root window
master.geometry("200x200")
# function to open a new window
# on a button click
def openNewWindow():
# Toplevel object which will
# be treated as a new window
newWindow = Toplevel(master)
# sets the title of the
# Toplevel widget
newWindow.title("New Window")
# sets the geometry of toplevel
newWindow.geometry("200x200")
# A Label widget to show in toplevel
Label(newWindow,
text ="This is a new window").pack()
label = Label(master,
text ="This is the main window")
label.pack(pady = 10)
# a button widget which will open a
# new window on button click
btn = Button(master,
text ="Click to open a new window",
command = openNewWindow)
btn.pack(pady = 10)
# mainloop, runs infinitely
mainloop()
Python3
# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
class NewWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title("New Window")
self.geometry("200x200")
label = Label(self, text ="This is a new Window")
label.pack()
# creates a Tk() object
master = Tk()
# sets the geometry of
# main root window
master.geometry("200x200")
label = Label(master, text ="This is the main window")
label.pack(side = TOP, pady = 10)
# a button widget which will
# open a new window on button click
btn = Button(master,
text ="Click to open a new window")
# Following line will bind click event
# On any click left / right button
# of mouse a new window will be opened
btn.bind("
输出:
示例 2:这将是一个基于类的方法,在此我们将创建一个类,该类将派生 Toplevel 小部件类并表现得像一个顶层。当您想将一些其他属性添加到现有的 Toplevel 小部件类时,此方法将很有用。
此类的每个对象都将是一个顶级小部件。我们还将使用bind() 方法来注册点击事件。
Python3
# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
class NewWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title("New Window")
self.geometry("200x200")
label = Label(self, text ="This is a new Window")
label.pack()
# creates a Tk() object
master = Tk()
# sets the geometry of
# main root window
master.geometry("200x200")
label = Label(master, text ="This is the main window")
label.pack(side = TOP, pady = 10)
# a button widget which will
# open a new window on button click
btn = Button(master,
text ="Click to open a new window")
# Following line will bind click event
# On any click left / right button
# of mouse a new window will be opened
btn.bind("
输出: