在这个GUI应用程序中,simpleapp是主窗口。作为初始化simpleapp的一部分,我试图设置应用程序的主目录,使用类setHomeDir打开一个窗口,以便我可以确认/设置当前的工作目录。首先,我无法让当前的工作目录显示在setHomeDir窗口中,即我无法让import Tkinter, os
class simpleapp(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.geometry('500x300+200+50')
self.home_dir=os.getcwd()
self.t=setHomeDir(parent=self, home=self.home_dir)
class setHomeDir(Tkinter.Tk):
def __init__(self,parent, home):
Tkinter.Tk.__init__(self)
self.parent = parent
self.geometry('500x100+200+50')
self.title('Set Home Directory')
self.grid()
#Input Box
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self, textvariable=self.entryVariable)
self.entryVariable.set(home)
self.entry.grid(column=0,row=0,sticky='EW')
self.grid_columnconfigure(0,weight=1)
self.resizable(True,False)
if __name__ == "__main__":
app = simpleapp(None)
app.title('Main Window')
app.mainloop()
工作。
我在哪里错了?
{
"Data": {
"connection-1": {
"ConnectionString": "Server=..."
},
"connection-2": {
"ConnectionString": "Server=..."
}
}
}
答案 0 :(得分:0)
此外,您不需要多个Tk()实例。当有多个实例时会感到困惑,所以请改用Toplevel。
import Tkinter, os
class SimpleApp():
def __init__(self,parent):
self.parent = parent
self.parent.geometry('500x300+200+50')
self.parent.title('Main Window')
self.home_dir=os.getcwd()
## pass same Tkinter instance to the class
SetHomeDir(parent=self.parent, home=self.home_dir)
class SetHomeDir():
def __init__(self,parent, home):
self.parent = parent
self.top=Tkinter.Toplevel(self.parent)
self.top.geometry('500x100+200+500')
self.top.title('Set Home Directory')
#Input Box
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self.top, textvariable=self.entryVariable)
self.entryVariable.set(home)
self.entry.grid(column=0,row=0,sticky='EW')
self.entry.focus_set()
self.top.grid_columnconfigure(0,weight=1)
self.top.resizable(True,False)
if __name__ == "__main__":
root=Tkinter.Tk()
app = SimpleApp(root)
root.mainloop()