【问题标题】:Tkinter python GUI issueTkinter python GUI问题
【发布时间】:2015-09-18 20:22:25
【问题描述】:

我正在尝试为我的学期项目创建一个菜单,但遇到了问题。

我想要一个用于我的控件的左窗格,并且在右侧会有可视化,但是当我运行代码时,由于 t1.pack() 和 c1.pack() 在左上角区域创建了一个不需要的空间

请帮助我如何获得两个清晰的窗格。

如果您没有得到问题,请告诉我。

提前致谢:)

__author__ = 'sagar'
try:
    import tkinter as tk  # for python 3
except:
    #import Tkinter as tk  # for python 2
    from Tkinter import *
    import ttk as ttk
    import ctypes
    import tkMessageBox

user32 = ctypes.windll.user32
WIDTH,HEIGHT = user32.GetSystemMetrics(0),user32.GetSystemMetrics(1)
WIDTH -= 50
HEIGHT -= 75

class Application: #pygubu.TkApplication
    def __init__(self,root):
        self.root = root

        self.fr_main = Frame(self.root,background="grey",highlightcolor="red")
        self.fr_main.grid(row=0,column=0,ipadx=(5*WIDTH)/100,ipady=HEIGHT)
        # self.fr_main.pack()

        self.btn_Simulator = Button(self.fr_main,text="Simulator")
        self.btn_Simulator.grid(row=0,column=0,pady=10)
        # self.btn_Simulator.pack()

        self.btn_Analysis = Button(self.fr_main,text="Analysis")
        self.btn_Analysis.grid(row=0,column=1,pady=10)
        # self.btn_Analysis.pack()


        self.fr_second = Frame(self.root,background="black",highlightcolor="red")
        self.fr_second.grid(row=0,column=1,ipadx=(40*WIDTH)/100,ipady=HEIGHT)
        self.nb_main = ttk.Notebook(self.fr_second)
        self.nb_main.pack(expand=1,fill=BOTH)


        f1 = Frame(self.nb_main)
        f2 = Frame(self.nb_main)

        self.nb_main.add(f2,text="Canvas")
        self.nb_main.add(f1,text="Calculation")

        t1 = Text(f1)
        c1 = Canvas(f2)
        c1.configure(background="black")
        t1.pack(expand=1,fill=BOTH)
        c1.pack(expand=1,fill=BOTH)


    def __controlState__(self,control,state):
        control.configure(state =state)

    def showName(self,Enableframe,DisableFrame):

        for child in Enableframe.winfo_children():
            Enableframe.config(relief = RAISED)
            self.__controlState__(child,"active")

        for child in DisableFrame.winfo_children():
            self.__controlState__(child,"disable")


if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.title("Network Simulator")


    root.geometry('%dx%d+%d+%d' % (WIDTH,HEIGHT,0,0))
    root.mainloop()

【问题讨论】:

    标签: python tkinter tkinter-canvas


    【解决方案1】:

    请帮助我如何获得两个清晰的窗格。

    让两个框架并排占据整个窗口高度的绝对最简单的方法是使用 pack 和适当的选项:

    self.fr_main.pack(side="left", fill="both", expand=True)
    self.fr_second.pack(side="left", fill="both", expand=True)
    

    关键是第一个要在左边还是右边,填“y”方向。之后,第二个可以在任何一侧,只要它填充两个方向即可。

    如果您想使用grid,请确保包含sticky 选项,以便框架填充给它们的空间:

    self.fr_main.grid(row=0, column=0, sticky="nsew")
    self.fr_second.grid(row=0, column=0, sticky="nsew")
    

    使用grid,您还需要赋予列权重,以便网格知道如何分配额外空间

    self.root.grid_rowconfigure(0, weight=1)
    self.root.grid_columnconfigure(0, weight=1)
    self.root.grid_columnconfigure(1, weight=1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 2016-12-04
      • 1970-01-01
      相关资源
      最近更新 更多