Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 StringVar.set(“…”)在多个tkinter窗口中不起作用_Python_Tkinter - Fatal编程技术网

Python StringVar.set(“…”)在多个tkinter窗口中不起作用

Python StringVar.set(“…”)在多个tkinter窗口中不起作用,python,tkinter,Python,Tkinter,我的程序有一个打开新窗口的库 这是名为make_entry的库: from tkinter import * def Create(): Window = Tk() # window Window.geometry("900x500+50+50") # heightxwidth+x+y mainPanel = Canvas(Window, width = 900, height = 500) # main screen mainPanel.pack()

我的程序有一个打开新窗口的库

这是名为make_entry的库:

from tkinter import *

def Create():
    Window = Tk() # window    
    Window.geometry("900x500+50+50") # heightxwidth+x+y

    mainPanel = Canvas(Window, width = 900, height = 500) # main screen
    mainPanel.pack()

    anyvar = StringVar() # the text in the entry
    entry = Entry(mainPanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
    mainPanel.create_window(200, 100, window = entry)
    anyvar.set("This doesnt work!!!!!")

    Window.mainloop()

#Create()
如果我自己运行这个库,那么一切都正常,但是当我从另一个程序导入它时,唯一不起作用的是anyvar.setThis不起作用

这里是我导入它的地方:大部分代码都被删掉了

from tkinter import *
Window = Tk()
import make_entry
make_entry.Create()
Window.mainloop()

有没有一种方法可以在不删除任何窗口的情况下解决此问题?

您有两个Tk实例,它们会混淆Tkinter。我猜StringVar将首先用于另一个实例。相反,将唯一的实例传递给函数,并为新窗口使用顶级

from tkinter import *

def Create(root):
    window=Toplevel(root)    
    window.geometry("900x500+50+50") # heightxwidth+x+y

    mainpanel = Canvas(window, width = 900, height = 500) # main screen
    mainpanel.pack()

    anyvar = StringVar() # the text in the entry
    entry = Entry(mainpanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
    mainpanel.create_window(200, 100, window = entry)
    anyvar.set("This doesnt work!!!!!")


您有两个Tk实例,它们混淆了Tkinter。我猜StringVar将首先用于另一个实例。相反,将唯一的实例传递给函数,并为新窗口使用顶级

from tkinter import *

def Create(root):
    window=Toplevel(root)    
    window.geometry("900x500+50+50") # heightxwidth+x+y

    mainpanel = Canvas(window, width = 900, height = 500) # main screen
    mainpanel.pack()

    anyvar = StringVar() # the text in the entry
    entry = Entry(mainpanel, width = 40, font = ("Purisa", 12, "bold"), justify = "center", textvariable = anyvar) # the entry
    mainpanel.create_window(200, 100, window = entry)
    anyvar.set("This doesnt work!!!!!")


不要使用从tkinter导入*,导入你需要的东西,那是从一个更大的程序,我有相当多的函数。还有,你怎么会有1个声誉,但有9个金徽章呢?不要使用tkinter import*,导入你需要的东西,那是从一个更大的程序中导入的,我在中有很多功能。还有,你是如何拥有1个声誉和9个金徽章的?@Tom Fuller:不仅有两个Tk实例,而且每个实例上都运行mainloop。每个程序应该有一个Tk实例和一个.mainloop调用。我的答案与Curly Joe的答案完全相同。@Tom Fuller:不仅有两个Tk实例,而且每个实例上都运行mainloop。每个程序应该有一个Tk实例和一个.mainloop调用。我的答案在其他方面与Curly Joe的完全相同。