我对在Python中使用Tkinter框架非常陌生。当我运行图形用户界面时,我发现以下post中的框架复选框不在同一行。

因此,我有以下两个问题:
1)是否可以在框架中使用"grid“,以便将小工具放置在我希望的位置?
2)是否可以将“相机白平衡”和“平均白平衡”放在同一行?在网格中是例如row=0、column=0和row=0、column=1
3)如果我使用下面的代码在"Input raw image file“按钮下面添加一行:
self.sep = Frame(self, height=2, width=450, bd=1, relief=SUNKEN)
self.sep.grid(row=1, column=0, columnspan=4, padx=5, pady=5)在不显示GUI的情况下,运行将永远有效。
我的代码是:
from __future__ import division
from Tkinter import *
import tkMessageBox
import tkFileDialog
class MainWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("FOO frame")
self.master.minsize(350, 150)
self.grid(sticky=W+N+S+E)
top_frame = Frame(self)
middle_frame = Frame(self)
bottom_frame = Frame(self)
top_frame.pack(side="top", fill="x")
middle_frame.pack(side="top", fill="x")
bottom_frame.pack(side="top", fill="x")
self.open = Button(top_frame,text="Input raw image file",
command=self.open,
activeforeground="red")
self.open.pack(side="left")
self.CheckVar_camera_white_balance = IntVar()
self.CheckVar_camera_white_balance = Checkbutton(middle_frame,
text="Camera white balance",
variable=self.CheckVar_camera_white_balance,
onvalue=1,
offvalue=0)
self.CheckVar_camera_white_balance.pack(side="top", fill="x")
self.CheckVar_average_whole_image_white_balance = IntVar()
self.CheckVar_average_whole_image_white_balance = Checkbutton(middle_frame,
text="Average white balance",
variable=self.CheckVar_average_whole_image_white_balance,
onvalue=1,
offvalue=0)
self.CheckVar_average_whole_image_white_balance.pack()
self.CheckVar_correct_chromatic_aberration = IntVar()
self.CheckVar_correct_chromatic_aberration = Checkbutton(middle_frame,
text="Correct chromatic aberration",
variable=self.CheckVar_correct_chromatic_aberration,
onvalue=1,
offvalue=0)
self.CheckVar_correct_chromatic_aberration.pack()
self.CheckVar_fix_dead_pixels = IntVar()
self.CheckVar_fix_dead_pixels = Checkbutton(middle_frame,
text="Fix dead pixels",
variable=self.CheckVar_fix_dead_pixels,
onvalue=1,
offvalue=0)
self.CheckVar_fix_dead_pixels.pack()
# functions
def open(self):
self.filename_open = tkFileDialog.askopenfilenames(defaultextension='.*')
if __name__ == "__main__":
d = MainWindow()
d.mainloop()发布于 2015-08-26 13:55:36
布莱恩·奥克利回答了你的问题。下面是一些代码,你可以想象他在说什么:
from __future__ import division
from Tkinter import *
import tkFileDialog
import tkMessageBox
from ttk import Separator
class MainWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("FOO frame")
self.master.minsize(350, 150)
self.grid(sticky=W+N+S+E)
top_frame = Frame(self)
middle_frame = Frame(self)
bottom_frame = Frame(self)
top_frame.pack(side="top", fill="x")
middle_frame.pack(side="top", fill="x")
bottom_frame.pack(side="top", fill="x")
self.open = Button(top_frame,text="Input raw image file",
command=self.open,
activeforeground="red")
self.open.pack(side="left")
self.sep = Separator(middle_frame, orient=HORIZONTAL)
self.sep.grid(row=0, column=0, columnspan=2, sticky="WE", pady=5)
self.CheckVar_camera_white_balance = IntVar()
self.CheckVar_camera_white_balance = Checkbutton(middle_frame,
text="Camera white balance",
variable=self.CheckVar_camera_white_balance,
onvalue=1,
offvalue=0)
self.CheckVar_camera_white_balance.grid(row=1, column=0, sticky="W")
self.CheckVar_average_whole_image_white_balance = IntVar()
self.CheckVar_average_whole_image_white_balance = Checkbutton(middle_frame,
text="Average white balance",
variable=self.CheckVar_average_whole_image_white_balance,
onvalue=1,
offvalue=0)
self.CheckVar_average_whole_image_white_balance.grid(row=1, column=1, sticky="W")
self.CheckVar_correct_chromatic_aberration = IntVar()
self.CheckVar_correct_chromatic_aberration = Checkbutton(middle_frame,
text="Correct chromatic aberration",
variable=self.CheckVar_correct_chromatic_aberration,
onvalue=1,
offvalue=0)
self.CheckVar_correct_chromatic_aberration.grid(row=2, column=0, columnspan=2, sticky="W")
self.CheckVar_fix_dead_pixels = IntVar()
self.CheckVar_fix_dead_pixels = Checkbutton(middle_frame,
text="Fix dead pixels",
variable=self.CheckVar_fix_dead_pixels,
onvalue=1,
offvalue=0)
self.CheckVar_fix_dead_pixels.grid(row=3, column=0, columnspan=2, sticky="W")
# functions
def open(self):
self.filename_open = tkFileDialog.askopenfilenames(defaultextension='.*')
if __name__ == "__main__":
d = MainWindow()
d.mainloop()如果您使用分隔符,则可以使用Seperator类,我在middle_frame中添加了分隔符,这样您就可以更好地控制这个小部件。现在你可以使用column来同时设置“相机白平衡”和“平均白平衡”。columns _ span属性定义小部件应该跨越的列数。因此,如果您有2个以上的列,请更新该属性。最后,如果您使用sticky,您可以定义小部件应该停留在哪一边。
发布于 2015-08-26 07:16:31
grid 要使事情保持一致,您应该阅读文档,以了解在调用grid和pack时所有可用的选项。这些选项允许您将项目居中或在边缘上全部对齐,控制填充等。
https://stackoverflow.com/questions/32215510
复制相似问题