Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在达到回调值后插入文本-Tkinter文本小部件_Python 2.7_Tkinter - Fatal编程技术网

Python 2.7 在达到回调值后插入文本-Tkinter文本小部件

Python 2.7 在达到回调值后插入文本-Tkinter文本小部件,python-2.7,tkinter,Python 2.7,Tkinter,我试图在缩放小部件达到某个值后,在Tkinter中插入文本到文本小部件。然而,似乎我的if语句不起作用,在按下calculate按钮后,文本小部件中没有插入任何内容 self.dU = Scale(self.f2, from_=100, to=-100) self.calculate_button = Button(self.f2, text="Calculate", command=self.calculate) self.results_display = Text(self.f3, he

我试图在缩放小部件达到某个值后,在Tkinter中插入文本到文本小部件。然而,似乎我的if语句不起作用,在按下calculate按钮后,文本小部件中没有插入任何内容

self.dU = Scale(self.f2, from_=100, to=-100)
self.calculate_button = Button(self.f2, text="Calculate", command=self.calculate)

self.results_display = Text(self.f3, height=10, width=62)
self.results_display.pack()
self.results_display.configure(state=DISABLED)

def results_text(self):
    if self.dU.get() > 0:
        self.results_display.configure(state=NORMAL)
        self.results_display.insert(END, "dU > 0, internal energy of the particles increases, therefore temperature increases.")
        self.results_display.configure(state=DISABLED)

def calculate(self):
    self.results_text()

在您的代码中,您在创建刻度大约一毫秒后执行
if
语句

如果希望在值更改时调用
If
语句,请配置标尺的
命令
参数

例如:

self.du = Scale(..., command=self.scale_changed)
...
def scale_changed(self, value):
    value = int(value)
    if value > 0:
        ...

注意:即使在这种情况下
self.du.get()
将返回一个整数,但当前值将作为字符串传递给
scale\u changed

if语句有效,但在程序启动时只调用一次。我猜您希望在每次滑块值更改时运行该检查。为此,您需要使用一个变量并对其进行跟踪:

    self.dU = tk.IntVar()
    self.dU.trace('w', self.check)
    s = tk.Scale(self, variable=self.dU, from_=100, to=-100)
    s.pack()

    self.results_display = tk.Text(self, height=10, width=62)
    self.results_display.pack()
    self.results_display.configure(state=tk.DISABLED)

def check(self, *args):
    if self.dU.get() > 0:
        self.results_display.configure(state=tk.NORMAL)
        self.results_display.insert(tk.END, "dU > 0, internal energy of the particles increases, therefore temperature increases.")
        self.results_display.configure(state=tk.DISABLED)

你需要更具体地描述一下你期望看到的情况,并解释实际发生的情况。我编辑了我的问题,请你再复习一遍。我想在按下“计算”按钮后将文本添加到文本小部件中。在这种情况下,当dU>0时,文本小部件中没有插入任何内容。