在Python中设置正确的Tkinter小部件位置

在Python中设置正确的Tkinter小部件位置,python,tkinter,widget,Python,Tkinter,Widget,我正在使用Tkinter为Python中的原始图像转换器编写GUI。GUI分为三部分。A部分有一个用于导入原始文件的按钮,B部分有四个复选按钮,C部分有两个输入空格的复选按钮 列0(=第一列)的长度由标签“正确色差”(最长元素)给出。这意味着,如果我更改名称,例如在“白平衡的正确色差”中,所有元素都会随着下图移动,并且A、B和C部分相互关联 我希望将A部分独立于B部分,以此类推,以获得下图。换句话说,我希望将每个按钮块放置到它们自己的框架中,以及主窗口中的框架中 原代码为: from __

我正在使用Tkinter为Python中的原始图像转换器编写GUI。GUI分为三部分。A部分有一个用于导入原始文件的按钮,B部分有四个复选按钮,C部分有两个输入空格的复选按钮

列0(=第一列)的长度由标签“正确色差”(最长元素)给出。这意味着,如果我更改名称,例如在“白平衡的正确色差”中,所有元素都会随着下图移动,并且A、B和C部分相互关联

我希望将A部分独立于B部分,以此类推,以获得下图。换句话说,我希望将每个按钮块放置到它们自己的框架中,以及主窗口中的框架中

原代码为:

from __future__ import division
from Tkinter import *
import tkMessageBox
import tkFileDialog

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("FOO converter")
        self.master.minsize(350, 150)
        self.grid(sticky=E+W+N+S)

        self.save_dir = None
        self.filename_open = None

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.CHART_FILE_TYPES = [('Pentax Electronic Format', '*.pef'),
                                ('Sony Alpha Raw', '*.arw'),
                                ('Minolta Raw', '*.mrw'),
                                ('Camera Image File Format', '*.crw'),
                                ('Canon Raw', '*.cr2'),
                                ('Epson Raw', '*.erw'),
                                ('Samsung Raw', '*.srw'),
                                ('Fujifilm Raw', '*.raf'),
                                ('Kodak Digital Camera Raw', '*.dcr'),
                                ('Nikon Electronic Format', '*.nef'),
                                ('Olympus Raw', '*.orf'),
                                ('All files', '.*')]

        for i in range(10): self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.open = Button(self, text='Input raw image file', command=self.open, activeforeground="red")
        self.open.grid(row=0, column=0, pady=2, padx=2, sticky=E+W+N+S)

        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)

        self.CheckVar_camera_white_balance = IntVar()
        self.CheckVar_camera_white_balance = Checkbutton(self,
            text="Camera white balance",
            variable=self.CheckVar_camera_white_balance,
            onvalue=1,
            offvalue=0)
        self.CheckVar_camera_white_balance.grid(row=2, column=0, pady=0, padx=0, sticky=W)

        self.CheckVar_average_whole_image_white_balance = IntVar()
        self.CheckVar_average_whole_image_white_balance = Checkbutton(self,
            text="Average the whole image for white balance",
            variable=self.CheckVar_average_whole_image_white_balance,
            onvalue=1,
            offvalue=0)
        self.CheckVar_average_whole_image_white_balance.grid(row=3, column=0, pady=0, padx=0, sticky=W)

        self.CheckVar_correct_chromatic_aberration = IntVar()
        self.CheckVar_correct_chromatic_aberration = Checkbutton(self,
            text="Correct chromatic aberration",
            variable=self.CheckVar_correct_chromatic_aberration,
            onvalue=1,
            offvalue=0)
        self.CheckVar_correct_chromatic_aberration.grid(row=4, column=0, pady=0, padx=0, sticky=W)

        self.CheckVar_fix_dead_pixels = IntVar()
        self.CheckVar_fix_dead_pixels = Checkbutton(self,
            text="Fix dead pixels",
            variable=self.CheckVar_fix_dead_pixels,
            onvalue=1,
            offvalue=0)
        self.CheckVar_fix_dead_pixels.grid(row=5, column=0, pady=0, padx=0, sticky=W)

        self.sep = Frame(self, height=2, width=450, bd=1, relief=SUNKEN)
        self.sep.grid(row=6, column=0, columnspan=4, padx=5, pady=5)

        self.CheckVar_brightness = IntVar()
        self.CheckVar_brightness = Checkbutton(self, text="Brightness",
            command=self.switch_brightness,
            variable=self.CheckVar_brightness,
            onvalue=1,
            offvalue=0)
        self.CheckVar_brightness.grid(row=7, column=0, pady=0, padx=2, sticky=W)

        self.label_level_brightness = Label(self, text="Brightness level:", state=DISABLED)
        self.label_level_brightness.grid(row=8, column=0, pady=0, padx=0, sticky=W)

        self.entry_level_brightness = Entry(self, state=DISABLED)
        self.entry_level_brightness.grid(row=8, column=1, pady=0, padx=0, sticky=W)

        self.label_gamma_curve = Label(self, text="Gamma curve:", state=DISABLED)
        self.label_gamma_curve.grid(row=9, column=0, pady=0, padx=0, sticky=W)

        self.entry_gamma_curve_1 = Entry(self, state=DISABLED)
        self.entry_gamma_curve_1.grid(row=9, column=1, pady=0, padx=0, sticky=W)

        self.entry_gamma_curve_2 = Entry(self, state=DISABLED)
        self.entry_gamma_curve_2.grid(row=9, column=2, pady=0, padx=0, sticky=E+W+N+S)


# functions

    def open(self):
        self.filename_open = tkFileDialog.askopenfilename(filetypes=self.CHART_FILE_TYPES, defaultextension='.*')

    def switch_brightness(self):
        pass

if __name__ == "__main__":
   d = MainWindow()
   d.mainloop()

如果希望区域独立,请为每个区域使用一个框架。例如:

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")
这样,您现在可以独立处理每个区域。这使您可以为不同的部分使用不同的几何管理器。您可能希望在顶部和中部框架中使用
pack
,在下部框架中使用网格

# Top frame
self.open = Button(top_frame, ...)
self.open.pack(side="left")
...
# Middle frame
self.CheckVar_camera_white_balance = Checkbutton(middle_frame, ...)
self.CheckVar_camera_white_balance.pack(side="top", fill="x")
...
# Bottom frame
self.CheckVar_brightness = Checkbutton(bottom_frame, ...)
self.CheckVar_brightness.grid(row=0, column=0, pady=0, padx=2, sticky=W)
...

我不太熟悉tkinter,但是在这种情况下,你可能想把每一块按钮放在它们自己的框架中,以及主窗口中的框架中。这篇文章可能会有帮助:例如:self.CheckVar\u brightness=Checkbutton(self…)是self.CheckVar\u brightness=Checkbutton(bottom\u frame…)?谢谢你的帮助。我老实说这对我来说不是很清楚(对不起),但这是一个很好的帮助