【发布时间】:2017-08-12 08:16:23
【问题描述】:
我想创建一个通过 GUI 执行以下操作的程序:
1.) 选择将在其中创建一些新目录的目录
2.) 使用条目小部件为这些新目录命名。
由于程序比较大,我想将我的代码分成几个模块,这会给我带来一些问题
到目前为止,我所做的是以下事情:
我有一个“主”程序 using.py
import os, sys
import Tkinter
import tkFileDialog
#import own modules
import Gui
root = Tkinter.Tk()
root.withdraw()
directory='some path'
tempdir = tkFileDialog.askdirectory(parent=root, initialdir=directory,
title='Please choose a location for your directory')
n2=Gui.DirectoryCreator()
n2.mainloop()
模块 Gui 看起来像这样
import Tkinter as tk
class DirectoryCreator(tk.Tk):
text=''
def __init__(self):
tk.Tk.__init__(self)
self.label=tk.Label(self, text='Enter the name of the directory')
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Name Of Folder",width=15, command=self.on_button)
self.label.grid(row=0,column=0)
self.button.grid(row=0,column=2)
self.entry.grid(row=0,column=1)
def on_button(self):
text= self.entry.get()
self.quit()
现在,关键是我需要在模块 using.py 中获取字符串“text”的值。如果我尝试 n2.text 之类的东西,则字符串 n2.text 只是空的,例如n2.text=""。我怎样才能得到这个字符串?
【问题讨论】:
-
当您尝试获取
n2.text时会发生什么?有错误吗?它是否返回""?究竟是什么不起作用? -
是的,它返回 n2.text=""。什么不起作用?我根本不知道如何从 using.py 中的条目小部件访问条目