【问题标题】:Passing user entry to another script将用户条目传递给另一个脚本
【发布时间】: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。非常感谢!

标签: python tkinter


【解决方案1】:

我相信你已经解决了这个问题,但我要为其他可能在这里找到方法的其他人备注信息:类函数中的 self 是调用 defs 时 python 将自动传递的实例. (除非您使用的是 classmethodstaticmethod,它们在您的用例中没有用,并且是一个稍微高级一点的主题)

# -- 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.aScript1.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()

但请注意,这可能无法跨多个模块扩展。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2013-08-27
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    相关资源
    最近更新 更多