如何命名从for循环创建的tkinter入口小部件?

时间:2019-05-26 21:14:29

标签: python user-interface tkinter

我是python和tkinter的新手,所以在命名在for循环内创建的条目小部件时遇到了麻烦。我的GUI的目标是为用户解决线性编程优化问题。我首先问用户问题中有多少个变量,假设输入了 n 个变量。然后,我创建一个for循环,该循环创建与每个变量相对应的 n 条目小部件,以存储该变量的系数值。我知道如何创建单个条目窗口小部件并命名它,以便可以通过 name .get()访问它。在我的代码中,我尝试创建一个字符串 name =“ x” + str(i),其中i是for循环递增变量。但是然后当我例如调用x1.get()时,我得到一个错误,即未定义x1。我该如何解决?并感谢您的提前帮助。

import tkinter as tkr

def objCos():
    x1Co = x1.get()
    print(x1Co)

def objFunction():
    global varFrame

    numOfVars = ofEntry.get()
    numOfVars = int(numOfVars)

    varFrame.destroy()
    varFrame = tkr.Frame(root)
    varFrame.pack(side = tkr.BOTTOM)

    varLabel = tkr.Label(varFrame, text = "Enter Objective Function Coefficients: ")
    varLabel.grid(row = 0, columnspan = 2)

    for i in range(1,numOfVars+1):
        strName = 'x' + str(i)
        strName = tkr.Entry(varFrame)
        strName.grid(row = i, column = 1)

        lbName = "varLabel" + str(i)
        lbName = tkr.Label(varFrame, text = "x"+ str(i) + " = ")
        lbName.grid(row = i, column = 0)

    varButton = tkr.Button(varFrame, text = "Update Objective Function Coefficients",
                           bg = "light grey", command = objCos)
    varButton.grid(row = numOfVars + 1, columnspan = 2)


root = tkr.Tk()

ofFrame = tkr.Frame(root)
ofFrame.pack(side = tkr.TOP)

global varFrame
varFrame = tkr.Frame(root)
varFrame.pack(side = tkr.BOTTOM)

ofLabel = tkr.Label(ofFrame, text = "Enter Number of Variables: ")
ofLabel.grid(row = 0, column = 0)

ofEntry = tkr.Entry(ofFrame)
ofEntry.grid(row =0, column = 1)

ofButton = tkr.Button(ofFrame, text = "Update Number of Variables", 
                      bg = "light grey", command = objFunction)
ofButton.grid(row = 1, columnspan = 2)

root.mainloop()

2 个答案:

答案 0 :(得分:2)

您可以拥有一个条目小部件列表。下面,我修改了部分代码,以将每个条目在创建后附加到entries

entries = []
for i in range(1,numOfVars+1):
    entries.append(tkr.Entry(varFrame))
    entries[-1].grid(row = i, column = 1) # Grid the last item in entries

    lbName = "varLabel" + str(i)
    lbName = tkr.Label(varFrame, text = "x"+ str(i) + " = ")
    lbName.grid(row = i, column = 0)

您可以使用该列表访问以后输入的数据。

顺便说一句,tkinter通常被导入为tk而不是tkr。它不会影响操作,但可以帮助其他人了解您所做的事情。

答案 1 :(得分:1)

例如,您可以使用简单的结构来保存vidgets,这是我的一个老函数中的一个示例:)

像那里一样,self.creation_panel是字典。

def creation_view(self, core_line, skills_line):
#### Navigate functions for main menu          ####
    def set_navigate(self, arg):
        self.navigate = arg

#### Configurations for create interference    ####
    self.configure_grid_x(20)
    self.configure_grid_y(5)
    row = 0
    col = 0

#### Create body of view                       ####
    for txt in core_line:
        if col == 12:
            col = 0
            row += 1
        self.creation_panel[txt] = Button(self.frame, text=txt, command=lambda cls=self, arg=txt: set_navigate(cls, arg))
        self.creation_panel[txt].grid(row=row, column=col, columnspan=3, sticky=NSEW)
        col += 3

    for txt in skills_line:
        if col == 12:
            col = 0
            row += 1
        self.creation_panel[txt] = Button(self.frame, text=txt, state=DISABLED, command=lambda cls=self, arg=txt: set_navigate(cls, arg))
        self.creation_panel[txt].grid(row=row, column=col, columnspan=4, sticky=NSEW)
        col += 4

    self.creation_panel["Preview"] = Button(self.frame, text="Preview", command=lambda cls=self: set_navigate(cls, "Preview"))
    self.creation_panel["Preview"].grid(row=row+1, column=0, columnspan=12, sticky=NSEW)

    self.preview_window1 = Label(self.frame, text="", justify=LEFT)
    self.preview_window1.grid(row=0, rowspan=5, column=12, columnspan=4, sticky=NSEW)
    self.preview_window1.config(state='disabled', relief=SUNKEN)

    self.preview_window2 = Label(self.frame, text="", justify=LEFT)
    self.preview_window2.grid(row=0, rowspan=5, column=16, columnspan=4, sticky=NSEW)
    self.preview_window2.config(state='disabled', relief=SUNKEN)

    self.frame.pack(side=LEFT, fill=BOTH, expand=1)

也许这不是最好的方法,还有其他方法可以纠正我。但是有一种可能的方法。

self.navigate变量用于导航,因为该类仅表示GUI的MVC模型中的视图:)。如果已更改,则从控制器发送信号,获取新模型并调用新视图。或类似的方式,我不清楚地记得那个旧项目。