User interface 在Tkinter和Pmw中放置小部件的迭代
通过使用Python中的循环在我的项目中创建、格式化和存储Pmw入口字段,而不是构建单个小部件,我大大减少了重复代码的数量。我甚至设法将它们以线性方式放置在根窗口上——在第0列中一个在另一个下面 但是,当我想将它们放在两列中时,我找不到生成以下小部件行和列坐标的方法 (0,0)(01)、(1,0)(1,1)(2,0)(2,1)来自for循环。我使用了一种变通方法,将上述坐标输入Python列表并引用该列表User interface 在Tkinter和Pmw中放置小部件的迭代,user-interface,python-3.x,tkinter,refactoring,pmw,User Interface,Python 3.x,Tkinter,Refactoring,Pmw,通过使用Python中的循环在我的项目中创建、格式化和存储Pmw入口字段,而不是构建单个小部件,我大大减少了重复代码的数量。我甚至设法将它们以线性方式放置在根窗口上——在第0列中一个在另一个下面 但是,当我想将它们放在两列中时,我找不到生成以下小部件行和列坐标的方法 (0,0)(01)、(1,0)(1,1)(2,0)(2,1)来自for循环。我使用了一种变通方法,将上述坐标输入Python列表并引用该列表 import tkinter import Pmw import time root
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循环中看到,我迭代了名为locations的列表,并将值放置在grid()选项的行、列属性中,以放置小部件
问题:
要彻底消除位置列表,有没有一种方法可以从for循环本身生成序列(0,0)(01)、(1,0)(1,1)(2,0)(2,1)所有情况下的一般答案都是可以使用for() 或增量计数器
this_row=0
this_column=0
for nextWidget in range(len(titles)):
...
this_column += 1
if this_column > 1:
this_row += 1
this_column = 0
或对nextWidget%2进行测试=与上述类似,但divmod的效果优于此模量
for ctr in range(6):
print divmod(ctr, 2)
(i/2,i%2)
(python2)或(i//2,i%2)
(python3)将为您提供所需的数字,如果您只需要两列
$ python3 -i
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(10):
... print((i//2, i%2))
...
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)
(3, 0)
(3, 1)
(4, 0)
(4, 1)
>>>
你是从零开始想出来的还是以前遇到过?如果你知道了,你能告诉我你是怎么解决的,因为我不好意思说我放弃了!我有了mod的想法,但从未想过要将其与div配对!Thankswaing使用divmod方法重构代码我开始认为我的原作对维护者来说不那么神秘-有什么意见吗?“lesscryptic”取决于谁维护它是一种价值判断。它是直截了当且显而易见的IMHO,如果有人不熟悉divmod,则可以打印结果。还要注意,您的解决方案不是通用的,也就是说,它只适用于一个特定的群体。是的,但我经常发现,在制作ďDatabase解决方案时,我通常只使用两种模式,即一列或两列GUI。感谢您的意见,这将有助于激发我课堂上的讨论。谢谢,我勾选了Curly Jo,因为他是最老的帖子,但我同样喜欢所有答案,它不允许我勾选两个答案。@TimothyLawman:我真的不在乎。选择一个有帮助的。我有一个问题贴出来,一年多来还没有人回答——如何通过按钮将Tkinter文本小部件的内容发送到本地打印机?你在日常工作中有没有这样做过?@TimothyLawman:没有,我已经十年没有在工作中使用打印机了。我假设有用于处理打印机的python库,但我从未使用过它们。好主意,我将看看是否有库,我从未想过要这样做
$ python3 -i
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> for i in range(10):
... print((i//2, i%2))
...
(0, 0)
(0, 1)
(1, 0)
(1, 1)
(2, 0)
(2, 1)
(3, 0)
(3, 1)
(4, 0)
(4, 1)
>>>