我有代码在Python Megawidgets和tkinter中创建一个笔记本。我相信重复代码的两个块可以迭代,而不是我使用的方法,即为每个小部件创建一行代码。
具体来说,在代码中如何遍历每个页面属性并在循环中配置每个选项卡颜色和字体?
代码
lass TextBookGUI:
def init (自我,主):
#place hash tables here
"""
Create 5 pages on using Pmw notebook widget.
Documenation for notebook:
http://pmw.sourceforge.net/doc/NoteBook.html
"""
Pmw.initialise(master)
self.nb = Pmw.NoteBook(master,borderwidth=2,arrownavigation=True,tabpos='n')
self.HomeTab = self.nb.add("Welcome")
self.nb.tab('Welcome').focus_set()
self.StudentTab = self.nb.add("Students")
self.BookTab = self.nb.add("Books")
self.LoanTab = self.nb.add("Loans")
self.HelpTab = self.nb.add("Help")
*self.nb._pageAttrs['Welcome']['tabreqwidth'] = 200
self.nb._pageAttrs["Welcome"]['tabreqheight'] = 100
self.nb._pageAttrs["Students"]['tabreqwidth'] = 200
self.nb._pageAttrs["Students"]['tabreqheight'] = 100
self.nb._pageAttrs["Books"]['tabreqwidth'] = 200
self.nb._pageAttrs["Books"]['tabreqheight'] = 100
self.nb._pageAttrs['Loans']['tabreqwidth'] = 200
self.nb._pageAttrs["Loans"]['tabreqheight'] = 100
self.nb._pageAttrs['Help']['tabreqwidth'] = 200
self.nb._pageAttrs["Help"]['tabreqheight'] = 100*
#format the house style of tabs: yellow bg and blue text
self.nb.component('Welcome-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Students-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Books-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Loans-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Help-tab').configure(font= ('Helviticva',20,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.nb.setnaturalsize()
违规区块是:
*self.nb._pageAttrs['Welcome']['tabreqwidth'] = 200
self.nb._pageAttrs["Welcome"]['tabreqheight'] = 100
self.nb._pageAttrs["Students"]['tabreqwidth'] = 200
self.nb._pageAttrs["Students"]['tabreqheight'] = 100
self.nb._pageAttrs["Books"]['tabreqwidth'] = 200
self.nb._pageAttrs["Books"]['tabreqheight'] = 100
我试图使用iterkeys,但是它产生了一个错误,即iterkeys不是这个特殊字典的功能。
另一个块是:
#format the house style of tabs: yellow bg and blue text
self.nb.component('Welcome-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Students-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Books-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Loans-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Help-tab').configure(font= ('Helviticva',20,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
肯定可以通过循环来减少重复代码?
答案 0 :(得分:2)
我建议使用字典来跟踪您的标签,而不是单个元素属性。然后,您可以循环创建选项卡:
self.tabs = {}
for tabname in (...):
self.tabs[tabname] = self.nb.add(tabname)
通过这种方式,您现在可以非常轻松地遍历所有选项卡:
for tabname in self.tabs.keys():
self.nb._pageAttrs[tabname]['tabreqwidth'] = 200
...
self.nb.component(tabname+"-tab').configure(...)