당신은 창 크기를 조정할 때 그래서 그들은 확장 할 수 있습니다 weight
을 가지고 행/열을 구성 할 것입니다. 또한 셀의 측면에 위젯을 sticky
까지 넣기를 원합니다. frame
행/열은 weight
이 될뿐만 아니라 모든 행과 열도 frame
안에 있으므로 for
루프가됩니다.
모든 Tkinter를 위젯 (내가 같은 동작으로 여러 항목을 필요로하는 프로그램을 가지고 템플릿 수 있도록 다음과 같이 처리 할 수
from Tkinter import *
#Sets up a frame
class MyApplication(Frame):
#When a class is initialized, this is called as per any class
def __init__(self, master):
#Similar to saying MyFrame = Frame(master)
Frame.__init__(self, master)
#Puts the frame on a grid. If you had two frames on one window, you would do the row, column keywords (or not...)
self.grid()
#Function to put the widgets on the frame. Can have any name!
self.create_widgets()
def create_widgets(self):
label = Label(self, text='Hello World!')
label.grid()
button = Button(self, text='Press Me!', command=self.hello)
button.grid()
def hello(self):
print "Hello World!"
root = Tk()
app = MyApplication(root)
root.mainloop()
(:
from Tkinter import *
class MyFrame(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid(row=0, column=0, sticky=N+S+E+W)
#Give the grid, column of the frame weight...
Grid.rowconfigure(master, 0, weight=1)
Grid.columnconfigure(master, 0, weight=1)
self.create_widgets()
def create_widgets(self):
#Give the grid, column of each widget weight...
for rows in xrange(3):
Grid.rowconfigure(self, rows, weight=1)
for columns in xrange(1):
Grid.columnconfigure(self, columns, weight=1)
self.label = Label(self, text="This is a test")
self.label.grid(row=0, column=0, sticky=N+S+E+W)
self.mytext1 = Text(self, width=30, height=5)
self.mytext1.grid(row=1, column=0, sticky=N+S+E+W)
self.mytext2 = Text(self, width=30, height=5)
self.mytext2.grid(row=2, column=0, sticky=N+S+E+W)
root = Tk()
app = MyFrame(root)
root.mainloop()
는 frame
는 다음과 같이 기본 보이는 당신의 클릭하면 기본 텍스트가 지워지고, 아무 것도 입력되지 않은 경우 반환됩니다.) 각 입력란에 바인딩을 추가하는 대신 동일한 작업을 수행하는 항목 클래스를 만들 수 있었으며 toplevels
(추가 창)을 쉽게 구현할 수있었습니다. 다음은 좀 더 복잡한 프로그램의 예입니다.
class IntroScreen(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.title('Intro Screen')
self.create_widgets()
self.focus_force()
def create_widgets(self):
label = Label(self, text='Hello World!')
label.grid()
button = Button(self, text='Open Window', command=self.newWindow)
button.grid()
def newWindow(self):
self.toplevel = InfoWindow()
#Like the frame, or any widget, this inherited from the parent widget
class InfoWindow(Toplevel):
def __init__(self):
Toplevel.__init__(self)
self.grid()
self.create_widgets()
self.focus_force()
def create_widgets(self):
label = Label(self, text='This is a window!')
label.grid()
root = Tk()
app = IntroScreen(root)
root.mainloop()
위에서 볼 수 있듯이이 기능을 통해 클래스가 없으면 훨씬 더 어려울 것입니다. Stackoverflow에 대한 더 많은 답변을 찾아보십시오 (나는 Bryan Oakley의 수많은 유익한 답변을 추천합니다). 그리고 Tkinter의 강력한 기능으로 나아갈 생각이라면 온라인으로 조사하십시오!
추신 : Switch between two frames in tkinter 여기
고마워요! 나는 GUI를위한 클래스를 사용하는 것이 더 쉬워 져야한다는 것을 알고 있지만 그것은 나 혼란을 일으킨다. – Lastalda
Tkinter로 클래스 사용법을 이해하려면 상속에 대해 잘 알아야합니다.그러나, Tkinter에서 클래스를 사용하지 않는 예제를 작성하는 방법의 예는 깨끗하고 읽기 쉽다고 생각합니다. Tkinter로 큰 응용 프로그램을 개발하는 방법을 묻지 않고 간단한 작업을 수행하는 방법을 설명했습니다. 대답이 간단할수록 더 많이 물었습니다. – tylerjw