如何使用 tkinter 创建两个按钮,当用户点击正确的按钮时,它将 运行 另一个 window
How to use tkinter to create two buttons and when user clicks the correct one, it will run another window
我正在为我们的策划者设计一个游戏项目 class 我决定使用 python tkinter,我想要一个功能,当用户按下正确的按钮时,会出现两个按钮,它将打开另一个 window 并显示其他三个选项(多选)。
我创建了两个按钮,但它们没有合并到文本 windows 中。我知道我需要一种在一个按钮中插入两个按钮的方法 window,但是添加的方法是什么?
root = Tk()
text = tk.Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
text.insert(tk.INSERT, 'This will test your chemistry ability. Enter y or n to start')
以上代码是要求用户输入 y 或 n 的介绍页面。那么我需要通过tk.Button
还是其他方式继续?
这是你想要的吗:
from tkinter import Tk, Label, Button
root = Tk()
Label(root, text='This will test your chemistry ability.').pack(padx=10, pady=10)
Button(root, text='Continue').pack(side='right', padx=10, pady=10)
Button(root, text='Cancel', command=root.quit).pack(side='right', padx=10, pady=10)
root.mainloop()
import tkinter as tk
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, MCQ):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Magic Button 1",
command=lambda: controller.show_frame(MCQ))
button.pack()
button2 = tk.Button(self, text="Magic Button 2",
command=lambda: controller.destroy())
button2.pack()
class MCQ(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="MCQ test!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Option1",
command=lambda: self.correspondingBehavior('Option1'))
button1.pack()
button2 = tk.Button(self, text="Option2",
command=lambda: self.correspondingBehavior('Option2'))
button2.pack()
button3 = tk.Button(self, text="Option3",
command=lambda: self.correspondingBehavior('Option3'))
button3.pack()
button4 = tk.Button(self, text="Quit",
command=lambda: controller.destroy())
button4.pack()
def correspondingBehavior(self,choice):
print(choice)
app = SeaofBTCapp()
app.mainloop()
from tkinter import Tk, Label, Button
root = Tk()
Label(root, text='This will test your chemistry ability.').pack(padx=10, pady=10)
def newWindow():
root.destroy()
root1 = Tk()
""" YOUR CODE FOR OTHER WINDOW"""
root1.mianloop()
Button(root, text='Continue',command=newWindow).pack(side='right', padx=10, pady=10)
Button(root, text='Cancel', command=root.quit).pack(side='right', padx=10, pady=10)
我正在为我们的策划者设计一个游戏项目 class 我决定使用 python tkinter,我想要一个功能,当用户按下正确的按钮时,会出现两个按钮,它将打开另一个 window 并显示其他三个选项(多选)。
我创建了两个按钮,但它们没有合并到文本 windows 中。我知道我需要一种在一个按钮中插入两个按钮的方法 window,但是添加的方法是什么?
root = Tk()
text = tk.Text(root, width=50, height=30, undo=True, autoseparators=False)
text.pack()
text.insert(tk.INSERT, 'This will test your chemistry ability. Enter y or n to start')
以上代码是要求用户输入 y 或 n 的介绍页面。那么我需要通过tk.Button
还是其他方式继续?
这是你想要的吗:
from tkinter import Tk, Label, Button
root = Tk()
Label(root, text='This will test your chemistry ability.').pack(padx=10, pady=10)
Button(root, text='Continue').pack(side='right', padx=10, pady=10)
Button(root, text='Cancel', command=root.quit).pack(side='right', padx=10, pady=10)
root.mainloop()
import tkinter as tk
LARGE_FONT= ("Verdana", 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, MCQ):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = tk.Button(self, text="Magic Button 1",
command=lambda: controller.show_frame(MCQ))
button.pack()
button2 = tk.Button(self, text="Magic Button 2",
command=lambda: controller.destroy())
button2.pack()
class MCQ(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="MCQ test!!!", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Option1",
command=lambda: self.correspondingBehavior('Option1'))
button1.pack()
button2 = tk.Button(self, text="Option2",
command=lambda: self.correspondingBehavior('Option2'))
button2.pack()
button3 = tk.Button(self, text="Option3",
command=lambda: self.correspondingBehavior('Option3'))
button3.pack()
button4 = tk.Button(self, text="Quit",
command=lambda: controller.destroy())
button4.pack()
def correspondingBehavior(self,choice):
print(choice)
app = SeaofBTCapp()
app.mainloop()
from tkinter import Tk, Label, Button
root = Tk()
Label(root, text='This will test your chemistry ability.').pack(padx=10, pady=10)
def newWindow():
root.destroy()
root1 = Tk()
""" YOUR CODE FOR OTHER WINDOW"""
root1.mianloop()
Button(root, text='Continue',command=newWindow).pack(side='right', padx=10, pady=10)
Button(root, text='Cancel', command=root.quit).pack(side='right', padx=10, pady=10)