制作一个在 Tkinter 中打开另一个 Window 的按钮
Make A Button That Opens Another Window in Tkinter
我有 2 个 windows。
GUI 和信息
我想在 GUI 上创建一个按钮来打开 window 信息,但我不知道该怎么做。
我尝试使用 TopLevel 小部件,但我不知道如何正确实现它。
这是 GUI 的代码
from tkinter import *
from automata.fa.dfa import DFA
from DFA import arranca
def btn_clicked():
print("Button Clicked")
def submit():
canvas.itemconfig(Canv, text="\t"+arranca(entry1.get()))
window = Tk()
window.geometry("880x550")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 550,
width = 880,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
这是信息代码
from tkinter import *
window = Tk()
window.title("Information")
window.geometry("700x400")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 400,
width = 700,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background_1.png")
background = canvas.create_image(
350.0, 188.0,
image=background_img)
window.resizable(False, False)
window.mainloop()
此代码演示了如何创建带有按钮的 Tk
window 和 Toplevel
子 window。
您所要做的就是用您喜欢的小部件填充这些 windows。
import tkinter as tk
class GUI:
def __init__(self):
self.parent = tk.Tk()
self.parent.title("GUI Window")
# Button control
button = tk.Button(
self.parent, text = "New window", command = self.window)
button.grid(sticky = tk.NSEW)
# Define window size
self.parent.geometry("278x26")
def window(self):
try:
self.information.winfo_viewable()
except AttributeError as err:
self.information = tk.Toplevel(self.parent)
# Make Toplevel a child of parent
self.information.transient( self.parent )
self.information.title("Information Window")
# place all your widgets here
else:
self.information.focus_force()
if __name__ == "__main__":
app = GUI()
app.parent.mainloop()
我有 2 个 windows。 GUI 和信息
我想在 GUI 上创建一个按钮来打开 window 信息,但我不知道该怎么做。
我尝试使用 TopLevel 小部件,但我不知道如何正确实现它。
这是 GUI 的代码
from tkinter import *
from automata.fa.dfa import DFA
from DFA import arranca
def btn_clicked():
print("Button Clicked")
def submit():
canvas.itemconfig(Canv, text="\t"+arranca(entry1.get()))
window = Tk()
window.geometry("880x550")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 550,
width = 880,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
这是信息代码
from tkinter import *
window = Tk()
window.title("Information")
window.geometry("700x400")
window.configure(bg = "#ffffff")
canvas = Canvas(
window,
bg = "#ffffff",
height = 400,
width = 700,
bd = 0,
highlightthickness = 0,
relief = "ridge")
canvas.place(x = 0, y = 0)
background_img = PhotoImage(file = f"background_1.png")
background = canvas.create_image(
350.0, 188.0,
image=background_img)
window.resizable(False, False)
window.mainloop()
此代码演示了如何创建带有按钮的 Tk
window 和 Toplevel
子 window。
您所要做的就是用您喜欢的小部件填充这些 windows。
import tkinter as tk
class GUI:
def __init__(self):
self.parent = tk.Tk()
self.parent.title("GUI Window")
# Button control
button = tk.Button(
self.parent, text = "New window", command = self.window)
button.grid(sticky = tk.NSEW)
# Define window size
self.parent.geometry("278x26")
def window(self):
try:
self.information.winfo_viewable()
except AttributeError as err:
self.information = tk.Toplevel(self.parent)
# Make Toplevel a child of parent
self.information.transient( self.parent )
self.information.title("Information Window")
# place all your widgets here
else:
self.information.focus_force()
if __name__ == "__main__":
app = GUI()
app.parent.mainloop()