python - 按下按钮时 Tkinter GUI : Adding new entry boxes using . grid()

标签 python python-2.7 user-interface layout tkinter

当“添加条目”用于我的程序时,我正在尝试创建新的输入框。我确实意识到,当我单独运行“打包代码”时,它是有效的,但是当我与 grid() 中的现有程序结合使用时,当我运行程序时,窗口不会显示。

我也明白,当我在同一程序中使用 .grid() 进行其他操作时,我们不应该同时使用 .pack() 。因此,我的问题是,如何在网格中创建新的输入框。

我尝试在其他地方寻找,但他们都建议打包。例如:我看过here here等等等等,仅举几例;但找不到与我类似的东西。我想在第 3 行的当前输入框下方添加输入框。

我对 Python 有点陌生; (我在这个程序中使用Python 2.7和tkinter模块。非常感谢您的帮助! 我的简化错误代码如下:

from Tkinter import *
import tkFileDialog
import tkMessageBox
import Tkinter
import os

class Window:

 def __init__(self, master):

    self.filename3=""
    csvfile=Label(root, text="NUMBERS").grid(row=3, column=0)
    bar=Entry(master).grid(row=3, column=3)
    self.entryText3 = StringVar()
    self.bar = Entry(root, textvariable=self.entryText3).grid(row=3, column=3)

    #BUTTONS
    self.cbutton= Button(root, text="OK", command=self.process_csv)
    self.cbutton.grid(row=15, column=6, sticky = W + E)

####PROBLEM FROM HERE#####
    all_entries = []
    addboxButton = Button(root, text='ADD', fg="BLUE", command=self.addBox)
    addboxButton.pack()

#I have also tried replacing the last 2 lines with the following 2 lines instead but to no avail:     
#self.addboxButton = Button(root, text='ADD THA-ID', fg="BLUE", command=self.addBox)
#self.addboxButton.grid(row=3, column=6)

    frame_for_boxes = Frame(root)
    frame_for_boxes.pack()

 def addBox(self):
    print "ADD"
    next_row = len(all_entries)
    lab = Label(frame_for_boxes, text=str(next_row+1))
    lab.grid(row=next_row, column=0)
    ent = Entry(frame_for_boxes)
    ent.grid(row=next_row, column=0)
    all_entries.append( ent )

 def process_csv(self):
    #own program here
    print "program"
root = Tk()
window=Window(root)
root.mainloop()

最佳答案

除了您所说的问题之外,您的程序还存在一些问题:

  1. 在初始化程序 (__init__()) 内,您将小部件附加到 root,但该部件未在您的 Window 范围内定义> 类。解决此问题的合理方法是使用 Tk() 的实例。 , id est root id est masterWindow 类中,将其作为实例变量。这意味着您在初始化程序中要做的第一件事是:self.master = master。这将导致您必须将 __init__() 中出现的所有 root 替换为 self.master

  2. 要解决的第二个问题是您在问题标题中指定的问题:您不能混合使用 grid()pack() 布局管理器相同的小部件容器。你必须决定哪一个。由于您使用 grid() 放置了大部分小部件,因此我建议您摆脱 pack()。这意味着:

      例如,
    • addboxButton.pack() 变为 addboxButton.grid(row=0, column=1)
    • 例如,
    • frame_for_boxes.pack() 变为 frame_for_boxes.grid(row=0, column=0)
  3. 上一个列表项解决了该问题,但它会让您发现程序中相关的其他问题:

    • NameError:未定义全局名称“all_entries”
    • NameError:未定义全局名称“frame_for_boxes”

    这是因为这些小部件变量在 addBox() 函数的范围内无法访问。要解决此问题,您必须将这 2 个元素设置为实例变量。这意味着:

    • all_entries = [] 变为 self.all_entries = []
    • frame_for_boxes = Frame(root) 变为 self.frame_for_boxes = Frame(self.master) (记住我们用 替换了 root 1. 中的 self.master)

    此错误修复的后果是您必须在程序内部使用所有内容:

    • self.all_entries 改为 all_entries
    • self.frame_for_boxes 而不是 frame_for_boxes
  4. 出于可扩展性的原因,我认为您至少必须将其余的小部件作为实例变量(即在它们前面加上 self 关键字)

  5. 由于您的实际项目比您在此 MCVE 中展示的更复杂,我鼓励您采用 SaYa idiom创建和放置小部件元素时。这意味着您需要替换:

      csvfile=Label(root, text="NUMBERS").grid(row=3, column=0)
    

      self.csvfile = Label(self.master, text="NUMBERS")
      self.csvfile.grid(row=3, column=0)
    

    为了避免程序中出现意外错误,您必须对初始化程序中声明的其余小部件执行相同的操作。

  6. 还有其他一些事情我想提一下,但大多数都可以在 PEP8 上找到。

关于python - 按下按钮时 Tkinter GUI : Adding new entry boxes using . grid(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43063434/

相关文章:

python - 将 Json 文件读取为 Pandas Dataframe 错误

python - 使用 gpu 使用 opencv 测量图像清晰度

c - 如何使 Python 对象表现得像 numpy 数组?

user-interface - 为 Haskell 积极开发并有据可查的 GUI 工具

c++ - 打开文件以在 C++ 中显示内容

javascript - 当您将 Selenium 与 chromedriver 一起使用时,网站可以检测到吗?

python - Scrapy - 使用正则表达式选择 xpath

python - 如何在 Python 的一行中将多个值添加到列表中?

java - 在 GUI 中使用预定义的字符串

python - djangorest-framework单例ViewSet