【发布时间】:2019-08-17 14:53:37
【问题描述】:
我正在尝试使用从 tkinter 中的输入字段获取的变量到另一个脚本。
简而言之: 我想在另一个脚本的输入字段中使用用户的输入。这根本不起作用。
非常感谢任何帮助!
到目前为止,我尝试过 Script2:
from Script1 import App
test = App()
print(test.write_slogan(self))
TypeError: init() 缺少 1 个必需的位置参数:'master'
和
from Script1 import App
print(App.write_slogan())
write_slogan() 缺少 1 个必需的位置参数:'self'
和
from Script1 import App
print(App.write_slogan(self))
NameError: name 'self' 未定义
和
import Script1
print(Script1.App.a)
AttributeError:类型对象“App”没有属性“a”
脚本1:
from tkinter import *
class App:
a = 0
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.slogan = Button(frame,
text="Hello",
command=self.write_slogan)
self.slogan.pack(side=LEFT)
self.entry1 = Entry(root, width=15)
self.entry1.pack(side=LEFT)
self.importbutton = Button(frame,
text="import",
command=self.importing)
self.importbutton.pack(side=LEFT)
def write_slogan(self):
print ("Test!")
App.a = self.entry1.get()
print(App.a)
return App.a
def importing(self):
print('Import')
import Script2
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
脚本2:
import Script1
【问题讨论】:
-
"...如果我只导入一个类,则不应调用主循环":了解executing-modules-as-scripts
-
谢谢,这回答了我的第一个问题。关于第二个,我还是不明白:/
-
“我想在另一个脚本的输入字段中使用用户的输入。”:很遗憾,我在其中看不到可重现的示例你给这个问题。 Edit您的问题相应地扩展。
-
我想我现在明白了。刚刚发现类变量和实例变量之间的区别。感谢您的提示!
-
工作!!我从未考虑过将 self 传递给 Script2。非常感谢!