gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:32:25 24 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

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

enter image description here

原代码为:

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 ,并在下部框架中使用 grid 。

# 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)
...

关于python - 在 Python 中设置正确的 Tkinter 小部件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com