私はPython3.6でIDLEで書いている基本的なプログラムのためのいくつかのコードを手に入れました。私はユーザーが4つ、1つ、またはそれらの間の任意の組み合わせのいずれかのオプションを選択し、「Total」をクリックして選択されたすべてのオプションの価格/合計を見つけてメッセージボックスに表示するようにします。 Checkbutton
、tkinter
、MyGUI
などPython3.6チェックボックス、価格オプション
を使用して、私は完全にif
文と同様def do_this(self):
セクションで失われています。ここにコードがありますが、すべての洞察が認められるでしょう。
私たちが教えている方法は非常に基本的なものであり、私はすべての答えを可能な限り基本的なものにしたいと考えています。割り当てはこのように設計されています。私は、私はaccrossに来ているすべての助けをいくつかの変数やコマンドや何かが欠けていると思う
from tkinter import *
import tkinter.messagebox
class MyGUI:
def __init__(self):
self.main_window=Tk()
self.main_window.geometry('300x300+100+100')
self.main_window.title("Price Options")
self.frame1=Frame(self.main_window, width=280, height=240)
self.frame1.place(x=10, y=10)
self.choice1=IntVar()
self.choice2=IntVar()
self.choice3=IntVar()
self.choice4=IntVar()
#want this price $20
self.cb1=Checkbutton(self.frame1, text='Option1', font=('Arial',16),\
variable= self.choice1)
self.cb1.place(x=40, y=40)
#want this price $30
self.cb2=Checkbutton(self.frame1, text='Option2', font=('Arial',16),\
variable= self.choice2)
self.cb2.place(x=40, y=75)
#want this price $40
self.cb3=Checkbutton(self.frame1, text='Option3', font=('Arial',16),\
variable= self.choice3)
self.cb3.place(x=40, y=100)
#want this price $50
self.cb4=Checkbutton(self.frame1, text='Option4', font=('Arial',16),\
variable= self.choice4)
self.cb4.place(x=40, y=135)
self.button1=Button(self.frame1, text='Total', font=('Arial', 16),\
command=self.do_this)
self.button1.place(x=40, y=170)
mainloop()
def do_this(self):
if self.choice1.get():
#What to put
elif self.choice2.get()+self.choice1.get():
#What to put
elif self.choice3.get()+self.choice1.get()+self.choice2.get():
#Etc..
elif
self.choice4.get() + self.choice1.get()+ self.choice2.get()+ self.choice3.get():
#Etc..
tkinter.messagebox.showinfo('Total',)#<----- What to put
my_gui=MyGUI()
私はちょうど周りに私の頭をラップしたい、深く、私たちはまだクラスに触れたhaventは高い熟練した概念でありますこの。
おかげさまで、onvalue/offvalueは私のノートには載っていませんでした。 「自己」のないボタンを作ることを可能にするもの最初は、def __init __(self):?私たちはまだすべてのコードを書き出すように教えられていますが、ほとんどの時間に役立ちますが、効率を学ぶことができるのは、そのステップを理解している限り長いです。もう一度ありがとう。 – ragnerman
@ragnerman、この例では、 'self'変数は複数のメソッドに現れる変数です。私は「自己」をその方法にローカルにしなかったものから取り除いた。例を単純化するだけです。より多くのメソッドを追加するときに、これらの値の一部が必要な場合は、 'self'を追加してインスタンスに属するようにすることができます。 – cdlane