python - 将用户条目传递给另一个脚本
问题描述
我正在尝试使用从 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:名称“自我”未定义
和
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
解决方案
我相信你已经解决了这个问题,但我正在为其他可能在这里找到方式的其他人评论信息:self
类函数内是实例 python 将在调用def
s 时自动传递。(除非您使用的classmethod或staticmethod在您的用例中没有用,并且是一个稍微高级一点的主题)
# -- Script1 \/
class App:
def __init__(self, master):
# self here is automatically passed
# but you need to pass the "master" arg.
frame = Frame(master)
# ... Example:
self._value = 0
def set_value(self, val):
self._value = val
def get_value(self):
return self._value
# -- Use (probably in Scipt2)
master = Tk()
app = App(master)
print (app.get_value()) # Notice how we don't pass self
>>> 0
app.set_value("my new value") # This string is the "val" arg
print (app.get_value())
>>> my new value
Scipt1.App.a 问题
您遇到的主要问题很可能与 python 管理模块的方式有关。班级正在写信给App.a
inScript1.App
但不是Script2.Script1.App.a
。这是预期的行为,所以我建议改为尝试使用以下内容:
class App:
def __init__(self, master):
# ... Make your tk widgets as you already have
def set_entry_value(self, val):
self.entry1.set(val)
def get_entry_value(self):
self._last_recieved_entry_value = self.entry1.get()
# -- Script2 \/
# The if __name__ == '__main__' is not run on imported
# scripts, only the script python starts execution on
# ~$> python Script2.py
import Script1
if __name__ == '__main__':
root = Tk()
app = Script1.App(root)
# Possibly set a default?
app.set_entry_value("Default value")
root.mainloop()
# - Some time after the mainloop is over
my_lastest_value = root.get_entry_value()
这样,您就可以让对象的本地实例处理它们的内部值。如果您希望设置备用模块的类成员,那么这样做Script2
可能会奏效。
if __name__ == '__main__':
root = Tk()
app = Script1.App(root)
# Do something to set the entry
Script1.App.a = app.get_entry_value()
但请注意,这可能无法跨多个模块扩展。
推荐阅读
- ruby - 如何在 Rails 迁移中更新现有参考的选项
- java - https调用在直接从java调用时工作,但不能从oracle 12c内部工作
- python - 从 Github 导入模块以在 Python 中使用
- python - Python:我如何仅打印以特定字符开头的特定字符串?
- c++ - 如何让我的程序在对二维数组中的行进行排序时交换列?
- c++ - 在c ++中嵌入八度音 - 如何传递可变大小的向量
- ios - 在使用 PDFKit 打印到 PDF 之前调整 UIImage 的大小 – Swift/Xcode
- amazon-web-services - 如何使用 AWS CLI 或 boto3 倒回 AWS 粘合作业书签
- c - 我必须在 Xlib 中 XFree 显示指针吗?
- python - 具有特定功能的多处理