2017-10-26 19 views
1

私はPython3.6でIDLEで書いている基本的なプログラムのためのいくつかのコードを手に入れました。私はユーザーが4つ、1つ、またはそれらの間の任意の組み合わせのいずれかのオプションを選択し、「Total」をクリックして選択されたすべてのオプションの価格/合計を見つけてメッセージボックスに表示するようにします。 CheckbuttontkinterMyGUIなど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は高い熟練した概念でありますこの。

答えて

0

インスタンスで、onvalueoffvalueのオプションを設定することが、あなたがどこにいるかになる簡単な方法です。つまり、self.choice1.get()がブール値を返すのではなく、チェックされていない場合は0を返し、チェックされている場合は20を返します。そして、あなたは、単に変数を合計ではなく、それらをテストすることができます。

from tkinter import * 
import tkinter.messagebox 

FONT = ('Arial', 16) 

class MyGUI: 
    def __init__(self): 
     main_window = Tk() 

     main_window.geometry('300x300+100+100') 
     main_window.title("Price Options") 

     frame = Frame(main_window, width=280, height=240) 
     frame.place(x=10, y=10) 

     self.choice1 = IntVar() 
     self.choice2 = IntVar() 
     self.choice3 = IntVar() 
     self.choice4 = IntVar() 

     # want this price $20 
     cb1 = Checkbutton(frame, text='Option1', variable=self.choice1, \ 
      onvalue=20, offvalue=0, font=FONT) 
     cb1.place(x=40, y=50) 

     # want this price $30 
     cb2 = Checkbutton(frame, text='Option2', variable=self.choice2, \ 
      onvalue=30, offvalue=0, font=FONT) 
     cb2.place(x=40, y=75) 

     # want this price $40 
     cb3 = Checkbutton(frame, text='Option3', variable=self.choice3, \ 
      onvalue=40, offvalue=0, font=FONT) 
     cb3.place(x=40, y=100) 

     # want this price $50 
     cb4 = Checkbutton(frame, text='Option4', variable=self.choice4, \ 
      onvalue=50, offvalue=0, font=FONT) 
     cb4.place(x=40, y=125) 

     button = Button(frame, text='Total', command=self.do_this, font=FONT) 
     button.place(x=40, y=170) 

     mainloop() 

    def do_this(self): 
     total = self.choice1.get() + self.choice2.get() + \ 
      self.choice3.get() + self.choice4.get() 
     tkinter.messagebox.showinfo('Total', str(total)) 

my_gui = MyGUI() 

これはあなたの問題へ最高解決策ではないかもしれないが、それは非常に基本的なです。

+0

おかげさまで、onvalue/offvalueは私のノートには載っていませんでした。 「自己」のないボタンを作ることを可能にするもの最初は、def __init __(self):?私たちはまだすべてのコードを書き出すように教えられていますが、ほとんどの時間に役立ちますが、効率を学ぶことができるのは、そのステップを理解している限り長いです。もう一度ありがとう。 – ragnerman

+0

@ragnerman、この例では、 'self'変数は複数のメソッドに現れる変数です。私は「自己」をその方法にローカルにしなかったものから取り除いた。例を単純化するだけです。より多くのメソッドを追加するときに、これらの値の一部が必要な場合は、 'self'を追加してインスタンスに属するようにすることができます。 – cdlane

関連する問題