我决定用tkinter来显示我的朋友,但我遇到了一个问题,在代码的某一点上,当我单击no时,它仍然显示当您单击yes时会发生什么。这是我迄今所写的代码。
from tkinter import *
import tkinter.messagebox
root = Tk()
root.title("THE ULTIAMTE TEST OF STRENGTH")
root.geometry('375x50')
topframe = Frame(root)
topframe.pack(side=TOP)
bottomframe = Frame(root)
bottomframe.pack(side=BOTTOM)
def butt1(event):
tkinter.messagebox.showinfo("Narrator: WHAT HAVE YOU DONE", "Prepare your soul for the hardest question ever")
answer = tkinter.messagebox.askquestion("Mother:", "Just got back home from work. Have you cleaned your room?")
if answer == 'yes':
tkinter.messagebox.showinfo("Mother:", "Wow, you actually did something useful for once.")
tkinter.messagebox.askquestion("Mother:", "WAIT, have you done the dishes yet? ")
if answer == 'yes':
tkinter.messagebox.showinfo("Mother:", "I'm Surprised. You've never been this productive in your entire life. Good job.")
tkinter.messagebox.askquestion("Mother:", "WAIT ANOTHER MINUTE YOU FRAGILE CHILD. Have you done your laundry? ")
if answer == 'yes':
tkinter.messagebox.showinfo("Mother:", "Oh my, what a miracle! I can't believe you actually did what you were supposed to do!")
tkinter.messagebox.showinfo("Mother:", "I'm still gonna whoop ya")
tkinter.messagebox.showinfo("Narrator:", "You got whooped.")
if answer == 'no':
tkinter.messagebox.showinfo("Mother:", "Oh my goodness you trashcan of a child, if you don't do yo laundry within the next 3 seconds I'm shoving all ya clothes down ya throat, YA HEAR ME?")
tkinter.messagebox.showinfo("Narrator:", "It's been 3 seconds.")
if answer == 'no':
tkinter.messagebox.showinfo("Mother:", "Of course you haven't. There's only so much you can do before you become useless again.")
tkinter.messagebox.askquestion("Mother:", "Last Question. Have you done your laundry?")
if answer == 'yes':
tkinter.messagebox.showinfo("Mother:", "Fair enough, but you still have to do the dishes or else you gettin'' the greatest whoopin of yo life.")
tkinter.messagebox.showinfo("Narrator:", "Just do yo dang dishes")
if answer == 'no':
tkinter.messagebox.showinfo("Mother:", "Oh my goodness you trashcan of a child, if you don't do yo laundry within the next 3 seconds I'm shoving all ya clothes down ya throat, YA HEAR ME?")
tkinter.messagebox.showinfo("Narrator:", "It's been 3 seconds.")
if answer == 'no':
tkinter.messagebox.showinfo("Mother:", "YOU'RE SUCH A USELESS CHILD THERE'S NO POINT IN EVEN RAISING YOU AT THIS POINT")
tkinter.messagebox.askquestion("Mother:", "Have you at least done the dishes?")
def butt2(event):
tkinter.messagebox.showinfo("WHAT HAVE YOU DONE", "Prepare your soul for the hardest question ever")
answer = tkinter.messagebox.askquestion("Mother:", "Just got back home from work. Have you cleaned your room?")
if answer == 'yes':
tkinter.messagebox.showinfo("Mother:", "Wow, you actually did something useful for once.")
tkinter.messagebox.askquestion("Mother:", "WAIT, have you done the dishes yet? ")
if answer == 'no':
tkinter.messagebox.showinfo("Mother:", "YOU'RE SUCH A USELESS CHILD THERE'S NO POINT IN EVEN RAISING YOU AT THIS POINT")
tkinter.messagebox.askquestion("Mother:", "Have you at least done the dishes?")
button_1 = Button(root, text="BEWARE OF THIS BUTTON", bg = 'black', fg='red')
button_1.bind("<Button-1>", butt1)
button_1.pack(fill=X)
button_2 = Button(root, text="DON'T BE AWARE OF THIS BUTTON", bg='yellow', fg='green')
button_2.bind("<Button-2>", butt2)
button_2.pack(fill=X)
button_1.pack(side=TOP)
button_2.pack(side=BOTTOM)
root.mainloop()另外一件事是,当我点击第二个按钮时,什么也不会发生。
发布于 2018-02-17 22:56:24
在您的butt1()函数中,您有一次将这一行指向顶部:
answer = tkinter.messagebox.askquestion(...)但是,在此之后,您将不再将ask问句()函数中的新值分配给“答案”变量。这就是为什么一旦你说是,它将是是的,其余的功能。因此,您只需要在其余的ask问句()行前面添加“答案=”。
编辑了以解决第二个问题:
对于第二个按钮没有响应,这是由于您的绑定。更改这一行:
button_2.bind("<Button-2>", butt2)将Button-2更改为Button-1,这应该有效。按钮-1是为左键。按钮-2是中间点击。更多信息在这里的文件。
https://stackoverflow.com/questions/48846586
复制相似问题