Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 3.4.3 tkinter—程序在声明IntVar或任何其他tkinter数据类型时冻结_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 3.4.3 tkinter—程序在声明IntVar或任何其他tkinter数据类型时冻结

Python 3.4.3 tkinter—程序在声明IntVar或任何其他tkinter数据类型时冻结,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,上一个线程: 在长线程之后,问题似乎与root.mainloop()运行之前的IntVar声明有关。显然,一旦进行了语法更改,前面线程中的代码就可以与Python2.6.6一起使用(多亏了pm2ring)。您可以在上一个线程中看到完整的代码。以下是一个(大致)最小、完整且可验证的示例: import tkinter as tk class Thing(tk.Frame): def Switch(self): if self.anyVar.get():

上一个线程:

在长线程之后,问题似乎与root.mainloop()运行之前的IntVar声明有关。显然,一旦进行了语法更改,前面线程中的代码就可以与Python2.6.6一起使用(多亏了pm2ring)。您可以在上一个线程中看到完整的代码。以下是一个(大致)最小、完整且可验证的示例:

import tkinter as tk

class Thing(tk.Frame):

    def Switch(self):
        if self.anyVar.get():
            state = "disabled"
        else:
            state = "normal"
        print(state)

        self.entry.configure(state=state)

    def createWidgets(self):

        ### This is the problem.
        print("testbefore")
        self.anyVar = tk.IntVar()
        print("testafter")
        ### End of problem. testafter is never printed. BUT WHY?!?!

        tk.Label(self,text="Test",font=("Times New Roman",15)).grid(row=0,column=0,sticky="W",padx=5,pady=5)
        self.box = tk.Checkbutton(self,variable=self.anyVar,command=self.Switch)
        self.box.grid(row=1,column=1,sticky="W",padx=0,pady=5)
        self.entry = tk.Entry(self,width=2)
        self.entry.grid(row=2,column=1,sticky="W",padx=2,pady=5)

    def __init__(self, parent):

        tk.Frame.__init__(self, parent)
        self.pack()
        self.parent = parent
        self.createWidgets()

class Framework(tk.Frame):

    def __init__(self, parent):

        tk.Frame.__init__(self, parent)
        self.instances = []
        self.parent = parent
        thing = Thing(self)
        self.instances.append(thing)

    def Activity(self):

        self.Clear()
        self.instances[0].pack()


def Initialise(window):
    window.master = tk.Frame(window)
    window.master.grid()
    window.instances = Framework(window.master)
    window.instances.grid()

root = tk.Tk()
Initialise(root)
root.mainloop()
root.destroy()
代码将一直执行,直到到达self.anyVar=tk.IntVar(),此时程序将冻结,但不会给出错误消息。“testafter”从不打印。知道为什么吗?谢谢。

(注意:我的原始答案与
网格和
包的使用有关。该答案不正确。这是正确的答案)

您偶然发现了一个非常模糊的边缘案例,它在Python2.6中不存在,但至少在Python3的某些版本中存在

我正在3.4上测试它。在该版本中,变量处理代码中引入了一些新代码。您的代码会使新代码进入无限循环。问题在于您选择使用
master
作为小部件属性的名称。这是一个您正在覆盖的内置属性,导致代码进入无限循环

解决方法很简单:将
window.master
重命名为
master
之外的任何东西。例如:

window._master = tk.Frame(window)

漂亮的麦克维特。:)在Python2.6.6上,这与我预期的一样有效(一旦我将
import tkinter
更改为
import tkinter
)。如果将
self.pack()
更改为
self.grid(row=0,column=0)
中的
。\uu init\uu
方法?@PM2Ring No change:(这真是一个令人困惑的问题……太好了,完全解决了,非常感谢!先生,您是一个特金特神xD