变量更改后tkinter重写文本

时间:2017-03-20 12:26:18

标签: python tkinter

我有这个代码。我删除import tkinter并导入Project和其他代码以便于阅读。

from Project import *

class TechnologyIDE:
    def __init__(self, master):
        #Vytvoření master
        self.master = master

        #Settings Tk
        master.title('Technology IDE')

        #Frame
        frame = Frame(root)
        frame.pack()

        self.frameLEFT = Frame(master)
        self.frameLEFT.pack(side=LEFT,fill=Y)

        self.frameRIGHT = Frame(master)
        self.frameRIGHT.pack(side=RIGHT, expand=True, fill=BOTH)

        #Vytvoření menu
        menu = Menu(master)
        root.config(menu=menu)

        #Boxy
        self.showCodewitHLines()
        self.showProjectToolbar()
        self.showNavigator()

    def showProjectToolbar(self):
        actualProject = project.actualProject
        listbox = Listbox(self.frameLEFT, width=50)
        listbox.pack(side=TOP, anchor=W)
        if actualProject == "":
            listbox.insert(END, "None project opened")
        else:
            print("Vidim"+actualProject)
            listbox.delete(0, END)
            listbox.insert(END, actualProject)
            var = os.listdir(actualProject+"/")
            for va in var :
                listbox.insert(END,"    "+va)

    def showCodewitHLines(self):
        TextArea = Text()
        ScrollBar = Scrollbar(root)
        ScrollBar.config(command=TextArea.yview)
        TextArea.config(yscrollcommand=ScrollBar.set)
        ScrollBar.pack(side=RIGHT, fill=Y)
        TextArea.pack(expand=True, fill='both')

    def showNavigator(self):
        listbox = Listbox(self.frameLEFT,width=50)
        listbox.pack(side=TOP, anchor=W)
        listbox.insert(END, "NAVIGATOR")


    root = Tk()
    ide = TechnologyIDE(root)
    root.mainloop()

我在Project.py中有这个代码

class Project:
    def __init__(self):
        self.actualProject = ""

    def newProject(self):
        self.top = Toplevel()
        self.top.title("Creating new project")
        self.top.focus_set()

        self.projectname = Entry(self.top)
        self.projectname.pack()

        btnClose = Button(self.top, text="Close", command=self.top.destroy)
        btnCreate = Button(self.top, text="Create", command=self.createProject)
        btnCreate.pack()
        btnClose.pack()

    def createProject(self):
        self.newpath = "Projects/"+self.projectname.get()
        if not os.path.exists(self.newpath):
            os.makedirs(self.newpath)
            fn = open(self.newpath+"/"+"run.py", "w+")
            self.top.destroy()
            self.actualProject = self.newpath
            print("Project"+self.actualProject)

project = Project()

我有这个问题。 当我创建成功创建的新项目时,我会在文件中看到它。我希望它被加载到gui(showProjectToolbar)。所以我创建了一个新项目,它应该加载到变量中,并且gui在循环中,所以它应该在gui中加载和显示,但它永远不会。 我在这里给你照片以便更好地理解 enter image description here

1 个答案:

答案 0 :(得分:1)

每次调用Listbox时,您都不应创建并打包showProjectToolbar

TechnologyIDE __def__函数中创建它,就像那样:

self.listbox = Listbox(self.frameLEFT, width=50)
self.listbox.pack(side=TOP, anchor=W)

然后,当您创建一个新项目时,只需添加它:self.listbox.insert(END, actualProject)