问题描述
我正在为我们的策划班设计一个游戏项目,我决定使用 python tkinter,我想要一个功能,即会出现两个按钮,当用户按下正确的一个时,它会打开另一个窗口并显示另一个三个选项(多选)。
我创建了两个按钮,但它们没有合并到文本窗口中。我知道我需要一种在一个窗口中插入两个按钮的方法,但是添加的方法是什么?
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',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,**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,text="MCQ test!!!",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,pady=10)
def newWindow():
root.destroy()
root1 = Tk()
""" YOUR CODE FOR OTHER WINDOW"""
root1.mianloop()
Button(root,text='Continue',command=newWindow).pack(side='right',pady=10)