###前提・実現したいこと
PyhonでTkinterを用いてGUIアプリケーションを作成しています。
文房具の請求のプログラムなのですが、イメージはamazonなどの
通販サイトみたいな感じです。
いま、tkinterのSpinboxという機能を使って、商品の個数を選べるようにし、
その上下(▲▼)ボタン(またはキー入力)を押して個数を決めたときに、それと
連動してウィンドウの右側に、数値(値段と個数をかけたもの)を表示させたいのですが、
get()を使っても初期値の0が表示されるだけで、スピンボックスの中の値を
変化させても右側に表示される値は変わりませんでした。どうすればよいですか?
また、pythonでのクラスの作り方がよくわかっていなくて、クラスなしで
関数を作って実装しているのですが、クラスは作ったほうがいいのでしょうか?
###試したこと
Python
1 2from tkinter import * 3import sys 4 5# Base window 6window = Tk() 7 8wc = Canvas(window, width = 2000, height = 2000) 9wc.create_rectangle(0,0,2000,2000, fill = "paleturquoise") 10 11wc.place(x = 0, y = 0) 12 13# Setting the title 14window.title("文房具の請求") 15 16# Coloring canvas 17 18for x in range(10): 19 c = Canvas(window, width = 240, height = 150) 20 c.create_rectangle(0,0,240,150, fill = "aliceblue") 21 if x<= 4: 22 c.place(x = 30, y = 30 + x * 155) 23 else: 24 c.place(x = 300, y = 30 + (x - 5) * 155) 25 26 27# Set items & spinbox 28 29item = ["*","*","*","*","*","*","*","*","*","*"] 30for x in range(10): 31 i = Label(text = item[x], bg = "#82EBF7") 32 ko = Label(text = "個", bg = "aliceblue") 33 if x <= 4: 34 i.place(x = 30, y = 30 + x * 155) 35 ko.place(x = 115, y = 150 + x * 155) 36 else: 37 i.place(x = 300, y = 30 + (x - 5) * 155) 38 ko.place(x = 385, y = 150 + (x - 5) * 155) 39 40''' 41ついでに、以下のコードをfor文とかで簡単に書く方法ってありますか? 4210個の商品のひとつずつにspinboxで値を決められるようにしています。 43''' 44 45s1value = StringVar() 46s2value = StringVar() 47s3value = StringVar() 48s4value = StringVar() 49s5value = StringVar() 50s6value = StringVar() 51s7value = StringVar() 52s8value = StringVar() 53s9value = StringVar() 54s10value = StringVar() 55 56s1 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s1value) 57s2 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s2value) 58s3 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s3value) 59s4 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s4value) 60s5 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s5value) 61s6 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s6value) 62s7 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s7value) 63s8 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s8value) 64s9 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s9value) 65s10 = Spinbox(window, from_=0, to=100, width = 10, textvariable=s10value) 66 67s1.pack 68s2.pack 69s3.pack 70s4.pack 71s5.pack 72s6.pack 73s7.pack 74s8.pack 75s9.pack 76s10.pack 77 78def spin(w,x): 79 if x <= 4: 80 w.place(x = 40, y = 150 + x * 155) 81 else: 82 w.place(x = 310, y = 150 + (x - 5) * 155) 83 84spin(s1,0) 85spin(s2,1) 86spin(s3,2) 87spin(s4,3) 88spin(s5,4) 89spin(s6,5) 90spin(s7,6) 91spin(s8,7) 92spin(s9,8) 93spin(s10,9) 94 95# Set price 96 97price = ["1 円","2 円","3 円","4 円","5 円","6 円","7 円","8 円","9 円","10 円"] 98 99for x in range(10): 100 t = Label(text = price[x], bg = "aliceblue") 101 if x <= 4: 102 t.place(x = 30, y = 70 + x * 155) 103 else: 104 t.place(x = 300, y = 70 + (x - 5) * 155) 105 106# name 107 108name = ["商品名1","商品名2","商品名3","商品名4","商品名5","商品名6","商品名7","商品名8","商品名9","商品名10"] 109 110for x in range(10): 111 n = Label(text = name[x], bg = "aliceblue") 112 if x <= 4: 113 n.place(x = 30, y = 50 + x * 155) 114 else: 115 n.place(x = 300, y = 50 + (x - 5) * 155) 116 117# Set window 118 119window.geometry("1000x825+100+20") 120 121# Set subwindow 122 123''' 124以下のsubwindow(sb)が、上記でいうウィンドウの右側のことです。 125''' 126 127sb = Canvas(window, width = 400, height = 700) 128sb.create_rectangle(0,0,400,700, fill = "white") 129sb.place(x = 570, y = 50) 130 131text = Label(window, text="カート", bg = "#69D7E3" ) 132text.place(x=570, y=50) 133 134# Get price 135 136''' 137もし、▲が押されたら、get()した値(現在値:0)に1を足し、 138▼が押されたら、1を引く、といったことを書こうとしたのですが、 139buttonupとは何ですか?といったエラーがでてしまいます。 140 141ちなみにs1は商品1のことであり、r1は商品1の個数に商品1の値段を 142かけたものです。(値段設定は今は適当なため、1円ということにしています。) 143''' 144 145if s1.invoke == buttonup: 146 s1.get = s1.get + 1 147elif s1.invoke == buttondown: 148 s1.get = s1.get - 1 149 150r1 = int(s1.get()) * 1 151 152note = Label(window, text = r1, bg = "white" ) 153note.place(x=590, y=80) 154 155window.mainloop() 156
Traceback (most recent call last): File "C:/Users/admin/Desktop/HCI/memo12.py", line 123, in <module> if s1.invoke == buttonup: NameError: name 'buttonup' is not defined
###補足情報(言語/FW/ツール等のバージョンなど)
使用ツール:Python3.5.1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/04 02:39
2017/07/04 04:19 編集
2017/07/04 04:47