我想用Tkinter在python中创建一个简单的GUI。
我从stackoverflow搜索了很多链接,基于这些链接,我可以提供以下代码。但是,按钮不在最下面一行。我认为问题是因为帧大小不等于父窗口大小。框架大小仅取决于区域小部件的覆盖范围。
import Tkinter as tk
master=tk.Tk()
master.title("Title")
master.geometry("500x400")
class makeframe(object):
def __init__(self,i):
self.i=i
self.frame=tk.Frame(master, bg = 'blue', width = 400, height = 400)
if(self.i == 0):
self.first_frame()
elif(self.i == 1):
self.second_frame()
else:
self.third_frame()
def next(self):
self.frame.grid_forget()
p[self.i+1].frame.grid()
def back(self):
self.frame.grid_forget()
p[self.i-1].frame.grid()
def first_frame(self):
self.nextbutton = self.next_button("Add New")
self.nextbutton.place(relx=0.90, rely=0.99, anchor=tk.SE)
self.backbutton = self.prev_button("Run Predefined")
self.backbutton.grid(column=0,row=0, padx = 2, pady = 2)
var = tk.StringVar()
self.label = tk.Label(master, textvariable=var,
relief=tk.RAISED, bg='gray', fg='black')
var.set("Welcome to Title \n(A software for testing)")
def second_frame(self):
tk.Label(self.frame, text="INSAMPLE: ").grid(row=0)
tk.Label(self.frame, text="OUTSAMPLE: ").grid(row=1)
tk.Label(self.frame, text="RESULT DIRECTORY: ").grid(row=2)
tk.Label(self.frame, text="INPUT DIRECTORY: ").grid(row=3)
tk.Label(self.frame, text="INPUT FILENAME: ").grid(row=4)
tk.Label(self.frame, text="INDEX COLUMNNAME: ").grid(row=5)
tk.Label(self.frame, text="INPUT FILEEXTENSION: ").grid(row=6)
tk.Label(self.frame, text="COLUMNNAME: ").grid(row=7)
tk.Entry(self.frame).grid(row = 0, column = 1)
tk.Entry(self.frame).grid(row = 1, column = 1)
tk.Entry(self.frame).grid(row = 2, column = 1)
tk.Entry(self.frame).grid(row = 3, column = 1)
tk.Entry(self.frame).grid(row = 4, column = 1)
tk.Entry(self.frame).grid(row = 5, column = 1)
tk.Entry(self.frame).grid(row = 6, column = 1)
tk.Entry(self.frame).grid(row = 7, column = 1)
self.nextbutton = self.next_button("Next")
self.nextbutton.place(relx=0.90, rely=1.1, anchor=tk.SE)
self.backbutton = self.prev_button("Previous")
self.backbutton.place(relx=0.10, rely=1.1, anchor=tk.SW)
def third_frame(self):
tk.Label(self.frame, text="Method name: ").grid(row=0)
tk.Label(self.frame, text="Method name: ").grid(row=1)
tk.Label(self.frame, text="Method name: ").grid(row=2)
tk.Label(self.frame, text="Method name: ").grid(row=3)
tk.Label(self.frame, text="Method name: ").grid(row=4)
tk.Entry(self.frame).grid(row=0, column=1)
tk.Entry(self.frame).grid(row=1, column=1)
tk.Entry(self.frame).grid(row=2, column=1)
tk.Entry(self.frame).grid(row=3, column=1)
tk.Entry(self.frame).grid(row=4, column=1)
self.nextbutton = self.next_button("Create Strategies")
self.nextbutton.place(relx=0.90, rely=0.99, anchor=tk.SE)
self.backbutton = self.prev_button("Previous")
self.backbutton.place(relx=0.10, rely=0.99, anchor=tk.SW)
def next_button(self, string):
return tk.Button(self.frame, text=string,
bg='gray', fg='black', command = self.next)
def prev_button(self, string):
return tk.Button(self.frame, text=string,
bg='gray', fg='black', command = self.back)
n=3
p=[0]*n
for i in range(n):
p[i]=makeframe(i)
p[0].frame.grid()
p[-1].nextbutton.config(state=tk.DISABLED)
master.mainloop()
按钮似乎可以正常工作,但这是小部件的放置,这对我来说是真正的问题。我希望它们覆盖良好的空间,并在右下方和左下方居中,并放置按钮。我是第一次尝试gui。抱歉,问题很简单。