私はこのテキストウィジットをクリアして時間を割いています。私は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')
エラーを表示してください。テキストウィジェットからすべてを削除する正しい方法は、あなたが言うことにかなり近いです: 'self.text.delete(" 1.0 "、" end ")' –
それは問題です、私はエラーを取得していません。私はリスナーにprintステートメントを入れて、それが実行されているかどうかを確認します。しかし、私はエラーが発生しません – MarsTwo
コードを試して、コピー/貼り付け/実行する必要があります – MarsTwo