2016-12-09 7 views
0

スーパーシンプルな自動販売機の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) 

は、ここに私のコードです。

+0

あなたは間違ってインデントを持っています。 'root.mainloop()'はどこですか? – furas

+1

'' Radiobutton'の 'variable ='は、通常の変数ではなく、 'StringVar'を想定しています。 – furas

答えて

2

chocchoiceはすべてvariable=textvariable=StringVar()を期待しているため、StringVar()、ではない通常の変数でなければならIntVar()など

chocchoice = StringVar(value="s") 

そして、あなたはBTW .get()

c = chocchoice.get() 

を使用する必要があります:に新しい値を割り当てないため=を使用すると、機能でglobal chocchoiceを使用する必要はありません。


EDIT:短いバージョン

import tkinter as tk 

# --- functions --- 

def update(): 

    c = choc_choice.get() 

    if c in products: 
     price, name = products[c] 
     choc_price.set(price) 
     choc_name.set(name) 

# --- data --- 

products = { # key: [price, name], 
    "m": [60, "Mars"], 
    "t": [70, "Twix"], 
    "mw": [65, "Milky Way"], 
    "s": [80, "Snickers"], 
}  

# --- main --- 

root = tk.Tk() 

# variables (have to be after tk.Tk()) 

choc_choice = tk.StringVar() 
choc_name = tk.StringVar(value="- none -") 
choc_price = tk.IntVar() 
summary = tk.IntVar() 

# left side 

left_frame = tk.Frame(root) 
left_frame.grid(row=0, column=0, padx=5) 

tk.Label(left_frame, text="Please select a chocolate bar:").pack() 

for key, val in products.items(): 
    price, name = val 
    tk.Radiobutton(left_frame, text=name, value=key, anchor='w', 
      variable=choc_choice, command=update).pack(fill='x') 

# right side 

right_frame = tk.Frame(root) 
right_frame.grid(row=0, column=1, sticky='n', padx=5) 

tk.Label(right_frame, text="You have chosen:").pack() 
tk.Label(right_frame, textvariable=choc_name).pack() 
tk.Label(right_frame, textvariable=choc_price).pack() 

# start 

root.mainloop() 
+0

あなたのコードの短いバージョンを見てください。 – furas

関連する問題