2016-06-07 95 views
0

問題是真的很簡單,所有funcions.packs(fill = X)由於某種原因不起作用,並在另一方面,如果我使用.pack(fill = X,pady = 10) pady works和fill = X被忽略。我希望有人可以向我解釋爲什麼這會發生。Tkinter視覺與填充= X

from tkinter import * 
import json 
import time 
#opens file where pinger saves information about websites 
def openfile(): 
    d={} 
    with open('test.json', 'r') as f: 
     for line in f: 
      line_ = json.loads(line) 
      name = list(line_.keys())[0] 
      status = list(line_[name].keys())[0] 
      ip = line_[name][status] 
      d[name] = {'name':name, 'status':status, 'ip':ip} 
     f.close() 
     return d 
#GUI main class  
class GUI(Tk): 
    def __init__(self): 
     self.tk = Tk() 
     self.tk.configure(background='white') 
     self.tk.wm_state('zoomed') 
     self.label = {} 
     self.topFrame = Frame(self.tk, bg="white") 

     self.tk.after(5000, self.task) 
     self.title = Frame(self.tk, bg="white") 
     self.title.pack (side=TOP) 
     #self.topFrame.pack(side=TOP, expand=True) 
     self.topFrame.pack(side=TOP) 
     self.titlelbl= Label(self.title, text="Website Status: Green - Online, Red - Offline \n",bg="white", font=("Helvetica", 24)) 
     self.titlelbl.pack(fill=X,pady=10) 
    #Funcion which creates and updates frame 
    def task(self): 
     i = 0 
     list={} 
     list = openfile() 
     if self.label != {}:#deleting previous information that we could refresh 
      for ele in self.label: 

       self.label[ele].destroy() 
      self.label= {} 

     for elem in list:#creating visual status if server is online text is in color green and opsite red 
      if list[elem]["status"]=="true": 
       lb = Label(self.topFrame, text=list[elem]["name"], fg="green",bg="white", font=("Helvetica", 24)) 
      if list[elem]["status"]=="false": 
       lb = Label(self.topFrame, text=list[elem]["name"], fg="red",bg="yellow", font=("Helvetica", 24)) 
      lb.pack(fill=X,pady=10) 

      self.label[elem] = lb 

     self.tk.after(5000, self.task)#Program refresh main window works every 5s 
#forever loop 
Gui= GUI() 
Gui.mainloop() 

回答

1

你永遠不會展開/填充框架小部件,所以框架只有它所需的大小才能容納它所有的孩子。

填充選項將增加孩子的大小(填充),您將能夠看到差異。而fill沒有什麼東西填充它的小孩部件已佔用盡可能多的空間,因爲它可以在它的主人(self.topFrame

+0

謝謝解決它 – Sande