2016-07-11 21 views
1

テキストボックスウィジェットでコンボボックスの値を表示するにはどうすればよいですか?Pythonのコンボボックス選択値に従ってテキストボックスに特定の値を表示

ここで私のコーディングですが、私はイベント引数を置く必要があると言っていますので、私のテキストボックスウィジェットにコンボボックスの値を表示するにはどのようなイベント引数が必要ですか?

from tkinter import * 
from tkinter import ttk 
class Application: 

    def __init__(self, parent): 
     self.parent = parent 
     self.value_of_combo='A' 
     self.combo() 
     self.textArea() 

    def textArea(self,event): 
     self.value_of_combo = self.box.get() 
     print(self.value_of_combo) 
     thistextArea=Text(self.parent,height=50,wideth=50) 
     thistextArea.grid(column=0,row=1) 

    def combo(self): 
     self.box_value = StringVar() 
     self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values=('A', 'B', 'C'),state='readonly') 
     self.box.bind('<<ComboboxSelected>>',self.textArea) 
     self.box.current(0) 
     self.box.grid(column=0, row=0) 

root = Tk() 
app = Application(root) 
root.mainloop() 
+0

あなたがテキストエリアにテキストを挿入するために 'thistextArea.insertを()'使用してみましたか? – Delioth

答えて

1

問題は(textAreaに渡すためにどのイベント引数に関するものではありません)方法:あなたは、むしろ次のエラー修正する必要があります:すべての

  1. まず、__init__()textArea()への呼び出しを削除し、むしろそれはそれを必要とするcombo()です。
  2. textArea()の中には、コールバックcombo()が呼び出されるたびに新しいテキストウィジェットが作成されます。したがって、テキストウィジェットを作成して配置する2行をtextArea()からcombo()に移動する必要があります。
  3. これを修正するとアルゴリズムは簡単です:Comboboxウィジェットから値を選択するときは、Textウィジェットが空であるかどうかをチェックする必要があります。挿入する場合は、挿入する前に既存のテキストを削除しない場合は直接値を挿入します。

プログラム:ここ

が固定関連のエラーのソリューションです:

from tkinter import * 
from tkinter import ttk 
class Application: 

    def __init__(self, parent): 
     self.parent = parent 
     self.value_of_combo='A' 
     self.combo() 
     #self.textArea() <-- This has nothing to do here, remove it 

    def textArea(self, e): 
     self.value_of_combo = self.box.get() 
     print(self.value_of_combo) 
     # Get the content of the Text widget 
     r=self.thistextArea.get('1.0','1.end') 
     # If it is empty then insert the selected value directly 
     if not r: 
      self.thistextArea.insert(INSERT, self.value_of_combo) 
     # If not empty then delete existing text and insert the selected value 
     else: 
      self.thistextArea.delete('1.0','1.end')    
      self.thistextArea.insert(END, self.value_of_combo) 


    def combo(self): 
     self.box_value = StringVar()   
     self.box = ttk.Combobox(self.parent, textvariable=self.box_value,values=('A', 'B', 'C'),state='readonly') 
     self.box.bind('<<ComboboxSelected>>',self.textArea) 
     self.box.current(0) 
     self.box.grid(column=0, row=0)   
     self.thistextArea=Text(self.parent,height=50,width=50) 
     self.thistextArea.grid(column=0,row=1) 

root = Tk() 
app = Application(root) 
root.mainloop() 
関連する問題