2017-05-05 3 views
0

私はこのテキストウィジットをクリアして時間を割いています。私はPythonとそのGUI APIには新しいことを認めますが、私はそのドキュメントを読んで、Stack Overflowの提案を無駄にしました。Python - テキストのクリアとリセットwidgit - Tkinter

私が見た多くの人がお勧め:これはしかし、リスナーに動作していないself.text.delete(0.0, 'end')

。奇妙なことに、私がコンストラクタに置くとうまくいきます。私はリスナーからスタックトレースも取得しません。以下はコードです

import tkinter 
from tkinter import Text 

def main(): 
    CalculatorGUI(CalculatorController()) 

class CalculatorController: 
    def __init__(self): 
     self.ans = "0" 

    def calculate(self, textBox): 
     value = str("") 
     try: 
      inputValue = textBox.replace(",", "").replace(" ", "") 
      if inputValue[:1] in "-*/+%": 
       value = str(eval(self.ans + inputValue))[::-1] 
      else: 
       value = str(eval(inputValue))[::-1] 

      return self.makeHumanReadable(value) 
     except: 
      return "I cannot math that!" 

    def makeHumanReadable(self, stringValue): 
     if "." in stringValue: 
      decimal = stringValue[:stringValue.index(".")] 
      integer = stringValue[stringValue.index(".") + 1:] 
      self.ans = (decimal + "." + (','.join(integer[i:i+3] for i in range(0, len(integer), 3))))[::-1]\ 
             .replace(",", "").replace(" ", "") 
      print("Current answer is: " + self.ans) 
      return (decimal + "." + (','.join(integer[i:i+3] for i in range(0, len(integer), 3))))[::-1] 

     else: 
      self.ans = ','.join(stringValue[i:i+3] for i in range(0, len(stringValue), 3))[::-1] \ 
                  .replace(",", "").replace(" ", "") 
      return ','.join(stringValue[i:i+3] for i in range(0, len(stringValue), 3))[::-1] 


class CalculatorGUI: 
    def __init__(self, controller): 
     self.controller = controller 
     self.root = tkinter.Tk() 
     self.frame1 = tkinter.Frame(self.root) 
     self.frame2 = tkinter.Frame(self.frame1) 

     self.text = BetterText(self.frame1, height=1, borderwidth=0) 
     self.text.insert(1.0, "Enter a math statement:") 
#  self.text.delete(0.0, 'end') # If this is not commented out, it deletes 
             # the text but not when put in the listener 

#  self.text.clearAll()   # Same here 
     self.text.configure(state="disabled") 

     self.entry = tkinter.Entry(self.frame2, width = 30) 
     self.calcButton = tkinter.Button(self.frame2, text="Calculate", \ 
          command=self.calculate) 

     self.text.pack() 
     self.entry.pack() 
     self.calcButton.pack() 

     self.frame1.pack() 
     self.frame2.pack() 

     self.root.mainloop() 


    def calculate(self): 
     self.entry.delete(0, "end") 
     self.text.clearAll() # Does not work 
     if self.entry.get() != "": 
      self.text.insert("END", self.controller.calculate(self.entry.get())) 


main()  

いいえアイデア??? Python 3.4を使用する

EDIT:Textウィジェットを拡張し、clearAll()メソッドを作成しようとしました。コンストラクタでは動作しますが、リスナでは動作せず、エラーは発生しません。そのコードのどこかに問題があり、私はそれを見ないだろう。

class BetterText(Text): 
    def __init__(self, master=None, cnf={}, **kw): 
     Text.__init__(self, master, kw) 

    def clearAll(self): 
     self.delete(0.0, 'end') 
+0

エラーを表示してください。テキストウィジェットからすべてを削除する正しい方法は、あなたが言うことにかなり近いです: 'self.text.delete(" 1.0 "、" end ")' –

+0

それは問題です、私はエラーを取得していません。私はリスナーにprintステートメントを入れて、それが実行されているかどうかを確認します。しかし、私はエラーが発生しません – MarsTwo

+0

コードを試して、コピー/貼り付け/実行する必要があります – MarsTwo

答えて

2

問題は、text.configure(state = "disabled")を使用していて、ウィジェットに書き込もうとしているようです。 Pythonでは、テキストボックスを無効にすることは、ユーザーが編集できないことを意味しますが、できないことを意味します。あなたのコメントアウトされたコードが働いた理由は、あなたがテキストウィジェットを無効にした行の前に来たということでした。テキストウィジェットを変更しようとする前に、クリアメソッドでtext.configure(state = "normal")を追加して、完了したら状態を元に戻してください。それは私がそれを試してみたときに問題を解決するように見えました。

+0

それをした!ありがとう – MarsTwo

関連する問題