首页 > 解决方案 > 将用户条目传递给另一个脚本

问题描述

我正在尝试使用从 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

标签: pythontkinter

解决方案


我相信你已经解决了这个问题,但我正在为其他可能在这里找到方式的其他人评论信息:self类函数内是实例 python 将在调用defs 时自动传递。(除非您使用的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.ainScript1.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()

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


推荐阅读