首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >改进Python中Tkinter框架

改进Python中Tkinter框架
EN

Stack Overflow用户
提问于 2015-08-26 07:04:53
回答 2查看 382关注 0票数 0

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

因此,我有以下两个问题:

1)是否可以在框架中使用"grid“,以便将小工具放置在我希望的位置?

2)是否可以将“相机白平衡”和“平均白平衡”放在同一行?在网格中是例如row=0、column=0和row=0、column=1

3)如果我使用下面的代码在"Input raw image file“按钮下面添加一行:

代码语言:javascript
复制
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的情况下,运行将永远有效。

我的代码是:

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2015-08-26 13:55:36

布莱恩·奥克利回答了你的问题。下面是一些代码,你可以想象他在说什么:

代码语言:javascript
复制
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,您可以定义小部件应该停留在哪一边。

票数 1
EN

Stack Overflow用户

发布于 2015-08-26 07:16:31

  1. 是,可以在您希望的任何帧中使用grid
  2. 是,可以在同一行中放置“相机白平衡”和“平均白平衡”。
  3. 代码永远运行而不显示任何内容的原因是因为您同时使用了网格(用于分隔符)和pack (用于帧)。它们具有相同的父项,因此您只能使用其中一个。

要使事情保持一致,您应该阅读文档,以了解在调用gridpack时所有可用的选项。这些选项允许您将项目居中或在边缘上全部对齐,控制填充等。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32215510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档