【问题标题】:How do I assign a TextBox value to a variable in Python?如何将 TextBox 值分配给 Python 中的变量?
【发布时间】:2015-05-08 07:04:00
【问题描述】:
lbl1 = Label(root, text="Target:").pack()
box1 = Entry(root).pack()
lbl2 = Label(root, text="Port:").pack()
box2 = Entry(root).pack()

ps_target = #value given to box1

我试图让我给 box1 的值出现在我的“ps_target”变量上。我该怎么做呢?我在 Python 2.7.6 中使用 Tkinter 模块。

如果您需要更多详细信息,请告诉我,我会尽可能提供。

【问题讨论】:

  • ps_target .set(box1.get())
  • 文件 "/usr/lib/python2.7/lib-tk/Tkinter.py",第 1489 行,在 call 中返回 self.func(*args) 文件" test.py”,第 49 行,在 port_scan ps_target .set(ebox1.get()) NameError: global name 'ps_target' is not defined /:
  • 试试@Leon 分享的链接
  • 是 ps_target 只是一个将 box1 中的值串起来的变量吗?
  • 没有它在不同的字符串“def port_scan(self)”

标签: python python-2.7 tkinter


【解决方案1】:

有两个问题,首先你应该单独调用pack。而不是

box1 = Entry(root).pack()

你应该这样做

box1 = Entry(root)
box1.pack()

要从box 获取值,请调用get

box1.get()

【讨论】:

    【解决方案2】:

    Entry(root).pack() 表示在父窗口小部件root 中打包一个条目。这将无返回到box1。将 none 对象分配给新变量是没有意义的。 所以如果你想操作这个入口对象,你应该这样写:

    my_entry = Entry(root)
    my_entry.pack()
    # do anything you like to my_entry here like my_entry.winfo_width()
    

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2021-03-10
      • 2021-09-17
      • 1970-01-01
      相关资源
      最近更新 更多