Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter中按下给定按钮时,我无法触发随机函数_Python_Tkinter - Fatal编程技术网

Python 在Tkinter中按下给定按钮时,我无法触发随机函数

Python 在Tkinter中按下给定按钮时,我无法触发随机函数,python,tkinter,Python,Tkinter,我正在制作一个按钮,它将在Python的tkinter模块中显示一条消息 首先,按钮上有文本。单击时,将显示一个消息框。messagebox是一个“弹出”或“错误消息” 下面的代码将显示执行上述语句的示例函数 def joke1(): messagebox.showinfo(title = "There are three types of people in this world", message = "Those who can count and those who can't.

我正在制作一个按钮,它将在Python的tkinter模块中显示一条消息

首先,按钮上有文本。单击时,将显示一个消息框。messagebox是一个“弹出”或“错误消息”

下面的代码将显示执行上述语句的示例函数

def joke1():
    messagebox.showinfo(title = "There are three types of people in this world", message = "Those who can count and those who can't.")

root = Tk()
root.title("Joke Board 1.0 by Jamlandia")
root.iconbitmap(r"C:\Users\VMWZh\Downloads\Icons8-Ios7-Messaging-Lol.ico")
button = Button(text = "There are three types of people in this world", bg = '#42f474', fg = 'black', command = joke1)
test = Button()

button.grid(column = 0, row = 0)

test.grid(column = 1, row = 0)
root.mainloop()
我想不出一种方法来编码它,这样当你按下按钮时,它将运行与按钮上显示的笑话相关的函数,然后随机绑定到一个新函数,并将文本更改为与该函数相关的笑话

问题:按下按钮。。。随机。。。换成笑话

tkinter-Tcl/Tk的Python接口
Python教程和Python模块

按钮
上显示随机
笑话
单击,不使用
消息框

import tkinter as tk
import random

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.joke_index = 0
        self.jokes = [("There are three types of people in this world", "Those who can count and those who can't."),
                      ('Grew up with six brothers', 'That’s how I learned to dance–waiting for the bathroom'),
                      ('Always borrow money from a pessimist.', 'He won’t expect it back.')
                      ]
       
        self.label1 = tk.Label(self)
        self.label1.grid(row=0, column=0, pady=3)
        
        self.label2 = tk.Label(self)
        self.label2.grid(row=1, column=0, pady=3)

        button = tk.Button(self,
                           text='Show next Joke',
                           command=self.show_random_joke,
                           bg='#42f474', fg='black'
                           )
        button.grid(row=2, column=0, pady=3)

    def show_random_joke(self):
        v = -1
        while v == self.joke_index:
            v = random.randrange(0, len(self.jokes)-1)
        self.joke_index = v

        self.label1['text'], self.label2['text'] = self.jokes[self.joke_index]

if __name__ == "__main__":
    App().mainloop()

使用Python:3.5进行测试

请添加其余相关代码(按钮/消息框等)。您的问题中没有足够的信息提供帮助。请提供一个示例,说明您正在努力解决的部分问题。为什么要将按钮“随机绑定”到新功能?为什么不让按钮调用一个函数来显示笑话列表中的笑话?拥有多个功能的价值是什么?你是说
按钮[“text”]=“Hello World”
按钮[“command”]=功能\u Hello\u World