为什么我的 Tkinter 按钮没有执行我的功能?
Why is my Tkinter button not executing my function?
from tkinter import *
import random
from collections import Counter
root = Tk()
root.title("Random")
root.geometry("600x400")
root.resizable(False, False)
def open_saw():
saw_wn = Tk()
saw_wn.title("Random App - Spin a Wheel")
saw_wn.geometry("600x400")
saw_wn.resizable(False, False)
saw_wn.mainloop()
def open_coin():
c_wn = Tk()
c_wn.title("Random App - Flip a Coin")
c_wn.geometry("600x400")
c_wn.resizable(False, False)
Label(c_wn, text=" ").grid(row=0,
column=0)
Label(c_wn, text="Flip the coin below!", font=("Yu Gothic UI", 12)).grid(row=0, column=1)
Label(c_wn, text=' ').grid(row=1, column=1)
coin_values = ["Heads", "Tails"]
coin_face = random.choice(coin_values)
def flip():
if coin_face == "Heads":
Label(c_wn, text="Coin: Heads").place(anchor='s')
else:
Label(c_wn, text="Coin: Tails").place(anchor='s')
coin = Button(c_wn, text='coin', padx=100, pady=90, command=flip)
coin.place(relx=0.5, rely=0.5, anchor=CENTER)
c_wn.mainloop()
def open_average():
avg_wn = Tk()
avg_wn.title("Random App - Averages")
avg_wn.geometry("840x300")
Label(avg_wn, text=" ").grid(row=0, column=0)
avg_instruct = Label(avg_wn, text="Enter your values below to get the averages in mean, median, and mode(put a "
"space between commas")
avg_instruct.config(font=("Yu Gothic UI", 10))
avg_instruct.grid(row=0, column=1)
Label(avg_wn, text=" ").grid(row=1, column=0)
entry = Entry(avg_wn)
entry.grid(row=2, column=1)
def calculate():
list_data = entry.get().split(', ')
list_data = [float(i) for i in list_data]
mean = sum(list_data) / len(list_data)
Label(avg_wn, text='Mean').grid(row=5, column=0)
Label(avg_wn, text=str(mean)).grid(row=6, column=0)
list_data_len = len(list_data)
list_data.sort()
if list_data_len % 2 == 0:
median1 = list_data[list_data_len // 2]
median2 = list_data[list_data_len // 2 - 1]
median = (median1 + median2) / 2
else:
median = list_data[list_data_len // 2]
Label(avg_wn, text='Median: ' + str(median)).grid(row=5, column=1)
Label(avg_wn, text=median).grid(row=6, column=1)
list_data_for_mode = Counter(list_data)
get_mode = dict(list_data_for_mode)
mode = [k for k, v in get_mode.items() if v == max(list(list_data_for_mode.values()))]
if len(mode) == list_data_len:
get_mode = ["No mode found"]
else:
get_mode = [str(i) for i in mode]
Label(avg_wn, text="Mode: ").grid(row=5, column=2)
Label(avg_wn, text=get_mode[0]).grid(row=6, column=2)
Label(avg_wn, text=" ").grid(row=3, column=0)
Button(avg_wn, text='Enter', command=calculate).grid(row=4, column=1)
Label(root, text=" ").grid(row=0, column=0)
title = Label(root, text="Welcome to Random")
title.config(font=("Yu Gothic UI", 24))
title.grid(row=0, column=1)
button1 = Button(root, text=" Spin a wheel ", padx=80, pady=25, command=open_saw)
button1.place(x=2.25, y=100)
button2 = Button(root, text="Calculate mean, mode, median, and range", padx=20, pady=25, command=open_average)
button2.place(x=325, y=100)
button3 = Button(root, text="Flip a Coin", padx=125, pady=25, command=open_coin)
button3.place(x=2.25, y=200)
button4 = Button(root, text="Generate a number", padx=82, pady=25)
button4.place(x=325, y=200)
root.mainloop()
我想要函数显示正面或反面(随机)。相反,该函数不显示任何内容并忽略该函数。我也试过打印值而不是在 tkinter 上显示它,但它只显示正面而不显示反面。如果需要任何其他详细信息来解决我的问题,请发表评论,我将提供其他详细信息。
在函数 open_coin
中,当单击按钮 coin
时,您正在创建 label
但未在 place
中指定 x 和 y 坐标,并且每次按下按钮时也会创建。因此,创建一个标签来显示结果并使用 config
.
不断更改其文本
而random
函数只被调用一次,所以把它放在flip
函数中,每次都调用以获得新的随机硬币值。
.
.
CoinLabel = Label(c_wn, text="") #will be used to display the result
CoinLabel.place(relx=0.5, rely=0.2, anchor='s')
def flip():
coin_values = ["Heads", "Tails"]
coin_face = random.choice(coin_values)
print(coin_face)
if coin_face == "Heads":
CoinLabel.config(text="Coin: Heads")
else:
CoinLabel.config(text="Coin: Tails")
coin = Button(c_wn, text='coin', padx=100, pady=90, command=flip)
coin.place(relx=0.5, rely=0.5, anchor=CENTER)
.
.
from tkinter import *
import random
from collections import Counter
root = Tk()
root.title("Random")
root.geometry("600x400")
root.resizable(False, False)
def open_saw():
saw_wn = Tk()
saw_wn.title("Random App - Spin a Wheel")
saw_wn.geometry("600x400")
saw_wn.resizable(False, False)
saw_wn.mainloop()
def open_coin():
c_wn = Tk()
c_wn.title("Random App - Flip a Coin")
c_wn.geometry("600x400")
c_wn.resizable(False, False)
Label(c_wn, text=" ").grid(row=0,
column=0)
Label(c_wn, text="Flip the coin below!", font=("Yu Gothic UI", 12)).grid(row=0, column=1)
Label(c_wn, text=' ').grid(row=1, column=1)
coin_values = ["Heads", "Tails"]
coin_face = random.choice(coin_values)
def flip():
if coin_face == "Heads":
Label(c_wn, text="Coin: Heads").place(anchor='s')
else:
Label(c_wn, text="Coin: Tails").place(anchor='s')
coin = Button(c_wn, text='coin', padx=100, pady=90, command=flip)
coin.place(relx=0.5, rely=0.5, anchor=CENTER)
c_wn.mainloop()
def open_average():
avg_wn = Tk()
avg_wn.title("Random App - Averages")
avg_wn.geometry("840x300")
Label(avg_wn, text=" ").grid(row=0, column=0)
avg_instruct = Label(avg_wn, text="Enter your values below to get the averages in mean, median, and mode(put a "
"space between commas")
avg_instruct.config(font=("Yu Gothic UI", 10))
avg_instruct.grid(row=0, column=1)
Label(avg_wn, text=" ").grid(row=1, column=0)
entry = Entry(avg_wn)
entry.grid(row=2, column=1)
def calculate():
list_data = entry.get().split(', ')
list_data = [float(i) for i in list_data]
mean = sum(list_data) / len(list_data)
Label(avg_wn, text='Mean').grid(row=5, column=0)
Label(avg_wn, text=str(mean)).grid(row=6, column=0)
list_data_len = len(list_data)
list_data.sort()
if list_data_len % 2 == 0:
median1 = list_data[list_data_len // 2]
median2 = list_data[list_data_len // 2 - 1]
median = (median1 + median2) / 2
else:
median = list_data[list_data_len // 2]
Label(avg_wn, text='Median: ' + str(median)).grid(row=5, column=1)
Label(avg_wn, text=median).grid(row=6, column=1)
list_data_for_mode = Counter(list_data)
get_mode = dict(list_data_for_mode)
mode = [k for k, v in get_mode.items() if v == max(list(list_data_for_mode.values()))]
if len(mode) == list_data_len:
get_mode = ["No mode found"]
else:
get_mode = [str(i) for i in mode]
Label(avg_wn, text="Mode: ").grid(row=5, column=2)
Label(avg_wn, text=get_mode[0]).grid(row=6, column=2)
Label(avg_wn, text=" ").grid(row=3, column=0)
Button(avg_wn, text='Enter', command=calculate).grid(row=4, column=1)
Label(root, text=" ").grid(row=0, column=0)
title = Label(root, text="Welcome to Random")
title.config(font=("Yu Gothic UI", 24))
title.grid(row=0, column=1)
button1 = Button(root, text=" Spin a wheel ", padx=80, pady=25, command=open_saw)
button1.place(x=2.25, y=100)
button2 = Button(root, text="Calculate mean, mode, median, and range", padx=20, pady=25, command=open_average)
button2.place(x=325, y=100)
button3 = Button(root, text="Flip a Coin", padx=125, pady=25, command=open_coin)
button3.place(x=2.25, y=200)
button4 = Button(root, text="Generate a number", padx=82, pady=25)
button4.place(x=325, y=200)
root.mainloop()
我想要函数显示正面或反面(随机)。相反,该函数不显示任何内容并忽略该函数。我也试过打印值而不是在 tkinter 上显示它,但它只显示正面而不显示反面。如果需要任何其他详细信息来解决我的问题,请发表评论,我将提供其他详细信息。
在函数 open_coin
中,当单击按钮 coin
时,您正在创建 label
但未在 place
中指定 x 和 y 坐标,并且每次按下按钮时也会创建。因此,创建一个标签来显示结果并使用 config
.
而random
函数只被调用一次,所以把它放在flip
函数中,每次都调用以获得新的随机硬币值。
.
.
CoinLabel = Label(c_wn, text="") #will be used to display the result
CoinLabel.place(relx=0.5, rely=0.2, anchor='s')
def flip():
coin_values = ["Heads", "Tails"]
coin_face = random.choice(coin_values)
print(coin_face)
if coin_face == "Heads":
CoinLabel.config(text="Coin: Heads")
else:
CoinLabel.config(text="Coin: Tails")
coin = Button(c_wn, text='coin', padx=100, pady=90, command=flip)
coin.place(relx=0.5, rely=0.5, anchor=CENTER)
.
.