2017-03-21 2 views
3

저는이 문제에 대한 답변을 찾기 위해 여러 해 동안 스택 오버플로를 조사해 왔지만 작동 할 수있는 것이 아무것도 없으므로이 질문을하고 있습니다. 저는 세 개의 버튼과 레이블이있는 작은 프로그램을 가지고 있으며, 그리드에 있습니다. 나는 버튼이나 라벨의 크기 나 모양이 프레임에 비례해서 어떻게 유지 될지 궁금해하고있었습니다. 이미지 크기를 조정 한 것과 마찬가지로 모든 것이 같은 크기로 유지됩니다. 대답은 분명하다파이썬 3.6 - 프레임 크기에 따라 Tkinter 버튼의 크기를 조절합니다.

from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.init_window() 
     self.grid() 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 
     self.pack(fill = BOTH, expand = 1) 

     quitButton = Button(self, text = "Quit", command = self.client_exit, width = 10, height = 5) #Quit Button 
     quitButton.grid(row = 0, column = 0, sticky = W) 

     encryptModeButton = Button(self, text = "Encrypt", command = lambda: self.execute("decrypted.txt", "encrypted.txt", 1, 0), width = 10, height = 5) #Encrypt Button 
     encryptModeButton.grid(row = 0, column = 1, sticky = W) 

     decryptModeButton = Button(self, text = "Decrypt", command = lambda: self.execute("encrypted.txt", "decrypted.txt", 0, 1), width = 10, height = 5) #Decrypt button 
     decryptModeButton.grid(row = 0, column = 2, sticky = W) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.grid(row = 0, column = 3) 
root = Tk() 
root.geometry("610x80") 

app = Window(root) 
root.mainloop() 

죄송 경우, 이미

+1

난 당신이 요구하는지 확실히 모르겠어요. 창 크기를 더 크게 조정하면 모든 것이 성장하기를 원한다는 말입니까? –

+0

@BryanOakley 네 맞습니다. –

답변

2

There's a good tutorial to grid packerpack()을 시도 :

여기 내 코드입니다. "Handling Resize"를 스크롤하면 sticky 옵션을 사용하는 방법과 열/행 쌍의 weight을 구성하는 방법을 알게됩니다. 난 그냥 sticky=NSEWcolumnconfigure/rowconfigure을 추가하고 당신이 원하는 것처럼 작동하는 것 같다 - 보시다시피

from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.master.minsize(width=650, height=80) 
     self.configure(relief=RAISED, borderwidth=10) 
     self.init_window() 
     self.grid(sticky = NSEW) 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 
     # configure weights; note: that action need for each container! 
     self.master.rowconfigure(0, weight=1) 
     self.master.columnconfigure(0, weight=1) 
     self.rowconfigure(0, weight=1) 
     for i in range(4): 
     self.columnconfigure(i, weight=1) 

     quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button 
     quitButton.grid(row = 0, column = 0, sticky = NSEW) 

     encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button 
     encryptModeButton.grid(row = 0, column = 1, sticky = NSEW) 

     decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button 
     decryptModeButton.grid(row = 0, column = 2, sticky = NSEW) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.grid(row = 0, column = 3, sticky = NSEW) 



root = Tk() 
root.geometry("650x80") 

app = Window(root) 
root.mainloop() 

가 :

는 그럼 grid 포장하여 예를 해보자! 약점은 각 컨테이너를 구성해야한다는 것입니다.

하지만 여기서는 pack 관리자에서 더 직관적이며 동일한 역할 옵션 (예 : fillexpand)을 사용하고 있습니다.

from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.master.minsize(width=650, height=80) 
     self.configure(relief=RAISED, borderwidth=10) 
     self.init_window() 
     self.pack(fill=BOTH, expand=True) 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 

     quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button 
     quitButton.pack(fill=BOTH, side=LEFT, expand=True) 

     encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button 
     encryptModeButton.pack(fill=BOTH, side=LEFT, expand=True) 

     decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button 
     decryptModeButton.pack(fill=BOTH, side=LEFT, expand=True) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.pack(fill=BOTH, side=LEFT, expand=True) 



root = Tk() 
root.geometry("650x80") 

app = Window(root) 
root.mainloop() 

무엇을 사용 하시겠습니까! 크기 조정, 그리드 및 팩에 대한 좋은 주제가 있습니다! Take a look

일부 기타 유용한 링크 :

관련 문제