【发布时间】:2016-03-16 18:36:32
【问题描述】:
我通过在 Python 中使用循环在我的项目中创建、格式化和存储 Pmw EntryFields 而不是构建单独的小部件,从而大大减少了重复代码的数量。我什至设法将它们以线性方式放置在根窗口上——在第 0 列中一个在另一个之下。
但是,当我想将它们放在两列中时,我找不到生成以下小部件行和列坐标的方法
(0,0)(0 1),(1,0)(1,1)(2,0)(2,1) 来自 for 循环。我使用了一种解决方法,将上述坐标输入到 Python 列表中并引用该列表。
import tkinter
import Pmw
import time
root=tkinter.Tk() #Create a main GUI window
root.option_readfile('optionDB') #Override Windows default fonts
##root.resizable(width=False, height=False) #Fix the window size in stone
root.iconbitmap('logo.ico') #add logo
titles=['a', 'b','c', 'd','e','f'] #List ttitles of entry fields
**locations=[(0,0),(0,1),(1,0),(1,1),(2,0),(2,1)]** #Entry locations
entry ={} #Container for referencing entry fieleds
for nextWidget in range(len(titles)): #Buils widgets on the page
dummy_x= Pmw.EntryField(root, #widget properties
labelpos = 'w',
label_text = titles[nextWidget],
label_fg="white",
entry_width=10
)
Pmw.Color.changecolor(dummy_x.component("hull"),
background="blue")
**dummy_x.grid(row= locations[nextWidget][0],**
column=locations[nextWidget][1],
padx=10,pady=10
)
entry.update({titles[nextWidget]: dummy_x}) #add to container
root.mainloop() #run the venet loop
您可以在主 for 循环中看到我遍历 名为位置的列表 并将值放置在 grid() 选项的行、列属性中以放置小部件。
问题: 要完全消除位置列表,有一种方法可以生成序列 (0,0)(0 1),(1,0)(1,1)(2,0)(2,1 ) 来自 for 循环本身?
【问题讨论】:
标签: user-interface python-3.x tkinter refactoring pmw