スーパーシンプルな自動販売機のGUIを作成しようとしていますが、ラジオボタンを使用して選択した後で、ラベルを更新することができません。代わりに、ラジオボタンを押すと、何が選択されていても、ラベルはデフォルト値に設定されます。私はサイトを通じて見てきたが、特にこの問題を解決する何かを見つけることができませんでしたStringVarを使用してTkinterラベルを更新する
from tkinter import *
import tkinter as tk
#Native Functions
def f_Update():
global chocchoice
global price
c = chocchoice
if c == "m":
price = 60
chocprice.set(price)
chocname.set("You have chosen - Mars")
if c == "t":
price = 70
chocprice.set(price)
chocname.set("You have chosen - Twix")
if c == "mw":
price = 65
chocprice.set(price)
chocname.set("You have chosen - Milky Way")
if c == "s":
price == 80
chocprice.set(price)
chocname.set("You have chosen - Snickers")
root =tk.Tk()
#Let's declare some variables!
chocchoice = "s"
price = 0
chocprice = StringVar()
chocname = StringVar()
summary = StringVar()
#Actual Code
frame = tk.Frame(root,
height = 300,
width = 800)
frame.pack()
L1 = tk.Label(root,
text = "Please select a chocolate bar:",
bg = "white")
L1.place(x=10, y=10)
Radiobutton(root,
text="Mars",
padx = 10,
variable = chocchoice,
value = "m",
command = f_Update).place(x=10,y=40)
Radiobutton(root,
text="Twix",
variable = chocchoice,
value = "t",
command = f_Update).place(x=10, y=70)
Radiobutton(root,
text="Milky Way",
variable = chocchoice,
value = "mw",
command = f_Update).place(x=10, y=100)
Radiobutton(root,
text="Snickers",
variable = chocchoice,
value = "s",
command = f_Update).place(x=10, y=130)
L2 = tk.Label(root,
textvariable = chocname ,
bg = "white")
L2.place(x=350,y=10)
L3 = tk.Label(root,
textvariable = chocprice,
bg = "white")
L3.place(x=350,y=40)
:
は、ここに私のコードです。
あなたは間違ってインデントを持っています。 'root.mainloop()'はどこですか? – furas
'' Radiobutton'の 'variable ='は、通常の変数ではなく、 'StringVar'を想定しています。 – furas