Python 按下按钮Tkinter后显示标签

Python 按下按钮Tkinter后显示标签,python,tkinter,Python,Tkinter,我正在努力使我的魔幻8球模拟器在tkinter中的答案在按下计算按钮后出现。如果有办法解决这个问题,我将非常感激 def __init__(self, window): ''' Constructor ''' self.window = window self.window.protocol('WM_DELETE_WINDOW', self.safe_exit) self.width = 400 self.input1 = StringVar()

我正在努力使我的魔幻8球模拟器在tkinter中的答案在按下计算按钮后出现。如果有办法解决这个问题,我将非常感激

def __init__(self, window):
    ''' Constructor '''
    self.window = window 
    self.window.protocol('WM_DELETE_WINDOW', self.safe_exit)
    self.width = 400
    self.input1 = StringVar()
    self.result = StringVar()

    ''' Creates the introduction label '''
    intro_label = tk.Label(window, text="Welcome to the Magic 8 Ball Simulator. Ask the Magic 8 Ball a question and you shall receive an answer: ")
    intro_label.pack()

    ''' Creates the canvas '''
    self.canvas = tk.Canvas(self.window, bg='white',
                    width=self.width, height=self.width)
    self.canvas.pack()

    ''' Image borrowed from giphy.com'''
    self.canvas.image = tk.PhotoImage(file='C:/Users/jakem/OneDrive/Desktop/8Ball.gif')
    self.canvas.create_image(200,200, image=self.canvas.image,)

    ''' Creates a Second Input Label '''
    Question_label = tk.Label(window, text="Please enter your question: ")
    Question_label.pack()

    '''Allows user to enter a question'''
    Question_entry = tk.Entry(window, textvariable=self.input1, width=50)
    Question_entry.pack()

    ''' Returns an answer to the asked question '''
    answer_button = tk.Button(self.window, text='Ask the Magic 8 Ball', command= self.answer)
    answer_button.pack()

    ''' Label which determines the fate of the user '''
    Ball_label = tk.Label(window, text = "The Magic 8 Ball has determined your fate: ")
    Ball_label.pack()

    ''' Displays the result from a list of available options '''
    Result_label = tk.Label(window, text = '', width=25)
    Result_label.pack()

    ''' Returns an answer to the asked question '''
    Repeat_button = tk.Button(self.window, text='Ask another query', command=self.answer())
    Repeat_button.pack()

    self.terminated = False

    if answer_button == onClick:
        Result_label.config(textvariable = self.result)

    tk.Button(root, text="Quit", command=root.destroy).pack()

def safe_exit(self):
    ''' Turn off the event loop before closing the GUI '''
    self.terminated = True
    self.window.destroy()

def answer(self):
    ''' Returns a randomly selected answer '''
    result = random.choice(answer_list)
    self.result.set(result)

def __init__(self,window):    
    Result_label.config(textvariable = self.result)


#def repeat(self):
    #Question_label.set('')
    #Ball_label.set('')
而不是:

Result_label.config(textvariable = self.result)
尝试:


我发现你的代码有一些问题。首先,您定义了两次此方法:

def __init__(self,window):
接下来,您忘了在
StringVar()
前面加上
tk.StringVar()

此按钮的命令不起作用,因为您将
self.answer()
的结果传递给它,而不是
self.answer
本身:

Repeat_button = tk.Button(self.window, text='Ask another query', command=self.answer())
以下是我对您的程序进行的简化返工,以解决上述问题并使其基本正常工作,无需Magic 8 Ball图像:

import tkinter as tk
import random

CANVAS_WIDTH = 400

class Magic8Ball:

    def __init__(self, window):
        ''' Constructor '''

        self.input = tk.StringVar()
        self.result = tk.StringVar()
        self.memory = dict()
        self.terminated = False

        self.answer_list = ["Yes", "Maybe", "No"]  # I needed to put this somewhere

        # Create the introduction label
        tk.Label(window, text="Welcome to the Magic 8 Ball Simulator. Ask a question and you shall receive an answer:").pack()

        # Create the canvas
        self.canvas = tk.Canvas(window, width=CANVAS_WIDTH, height=CANVAS_WIDTH)
        self.canvas.pack()

        # Image borrowed from giphy.com
        # self.canvas_image = tk.PhotoImage(file='C:/Users/jakem/OneDrive/Desktop/8Ball.gif')
        # self.canvas.create_image(200, 200, image=self.canvas_image)

        # Create a Second Input Label
        question_label = tk.Label(window, text="Please enter your question:")
        question_label.pack()

        # Allow user to enter a question
        tk.Entry(window, textvariable=self.input, width=50).pack()

        # Return an answer to the asked question
        tk.Button(window, text='Ask the Magic 8 Ball', command=self.answer).pack()

        # Label which determines the fate of the user
        tk.Label(window, text="The Magic 8 Ball has determined your fate:").pack()

        # Display the result from a list of available options
        tk.Label(window, text='', width=25, textvariable=self.result).pack()

        tk.Button(window, text="Quit", command=root.destroy).pack()

    def answer(self):
        ''' Returns a randomly selected answer '''

        question = self.input.get()

        if question:  # don't answer if nothing's been asked
            if question in self.memory:
                result = self.memory[question]  # if a question is repeated,  be consistent
            else:
                result = random.choice(self.answer_list)
                self.memory[question] = result

            self.result.set(result)

root = tk.Tk()

app = Magic8Ball(root)

root.mainloop()

不要使用三重引号字符串作为一般注释机制。可用于提供特定信息并位于代码中特定位置的docstring。否则,请使用标准注释字符。

您的问题缺少导入、类2名称/如何调用类、根窗口信息/mainloop以及
答案列表中的内容。
Repeat_button = tk.Button(self.window, text='Ask another query', command=self.answer())
import tkinter as tk
import random

CANVAS_WIDTH = 400

class Magic8Ball:

    def __init__(self, window):
        ''' Constructor '''

        self.input = tk.StringVar()
        self.result = tk.StringVar()
        self.memory = dict()
        self.terminated = False

        self.answer_list = ["Yes", "Maybe", "No"]  # I needed to put this somewhere

        # Create the introduction label
        tk.Label(window, text="Welcome to the Magic 8 Ball Simulator. Ask a question and you shall receive an answer:").pack()

        # Create the canvas
        self.canvas = tk.Canvas(window, width=CANVAS_WIDTH, height=CANVAS_WIDTH)
        self.canvas.pack()

        # Image borrowed from giphy.com
        # self.canvas_image = tk.PhotoImage(file='C:/Users/jakem/OneDrive/Desktop/8Ball.gif')
        # self.canvas.create_image(200, 200, image=self.canvas_image)

        # Create a Second Input Label
        question_label = tk.Label(window, text="Please enter your question:")
        question_label.pack()

        # Allow user to enter a question
        tk.Entry(window, textvariable=self.input, width=50).pack()

        # Return an answer to the asked question
        tk.Button(window, text='Ask the Magic 8 Ball', command=self.answer).pack()

        # Label which determines the fate of the user
        tk.Label(window, text="The Magic 8 Ball has determined your fate:").pack()

        # Display the result from a list of available options
        tk.Label(window, text='', width=25, textvariable=self.result).pack()

        tk.Button(window, text="Quit", command=root.destroy).pack()

    def answer(self):
        ''' Returns a randomly selected answer '''

        question = self.input.get()

        if question:  # don't answer if nothing's been asked
            if question in self.memory:
                result = self.memory[question]  # if a question is repeated,  be consistent
            else:
                result = random.choice(self.answer_list)
                self.memory[question] = result

            self.result.set(result)

root = tk.Tk()

app = Magic8Ball(root)

root.mainloop()