2013-09-23 50 views
-1

作爲概念證明,此操作會創建Entry窗口小部件並將它們放入數組中。問題是我只在Windows上獲得24個而不是26個Entry小部件,3.3.1在Windows上,而在Lubuntu上只有3.3個。爲什麼?我該如何解決這個問題?使用for循環創建條目窗口小部件

EDIT2:簡化代碼只是問題的一部分http://i.imgur.com/u0OmcCI.pnghttp://pastebin.com/2791MFRu

from tkinter import * 

class test: 
    def __init__(self, root): 
     self.variables = [] 
     for i in range(26): 
      self.variables.append(StringVar()) 

     self.frames = [] 
     self.labels = [] 
     self.entrys = [] 
     for i in range(2): 
      self.frames.append(Frame(root)) 
      for ii in range(26): 
       char = str(chr(ord('A') + ii)) 
       if i == 0: 
        self.labels.append(Label(self.frames[0] , text = char)) 
        self.labels[-1].grid(padx=0, pady=0, row=ii, column=i) 
       else: 
        self.entrys.append(Entry(self.frames[1], textvariable =self.variables[ii])) 
        self.entrys[-1].grid(padx=0, pady=0, row=ii, column=i) 
      self.frames[i].grid(row = 0,column=i) 

root = Tk() 
root.geometry("200x600+50+50") 
T = test(root) 
root.mainloop() 
+0

你能不能給我們整個代碼,以便我們可以自己嘗試一下? –

+0

您的縮進似乎不正確。 –

+0

當我運行上面的代碼時,我得到了26個輸入小部件。你爲什麼覺得你少於26? –

回答

1

你有26項,但因爲每個人比其相應的標籤短,他們擠在一起更緊,去失相。

enter image description here

這是因爲你把兩列在自己的幀,每幀緊密對齊項目儘可能自己的網格內,忽視了其他列。

您可以完全去除柱框架,並直接把標籤和條目根得到正確對齊:

from tkinter import * 

class test: 
    def __init__(self, root): 
     self.variables = [] 
     for i in range(26): 
      self.variables.append(StringVar()) 

     self.labels = [] 
     self.entrys = [] 
     for ii in range(26): 
      char = str(chr(ord('A') + ii)) 
      self.labels.append(Label(root , text = char)) 
      self.labels[-1].grid(padx=0, pady=0, row=ii, column=0) 
      self.entrys.append(Entry(root, textvariable =self.variables[ii])) 
      self.entrys[-1].grid(padx=0, pady=0, row=ii, column=1) 

root = Tk() 
root.geometry("200x600+50+50") 
T = test(root) 
root.mainloop() 

結果:

enter image description here