Я создаю графический интерфейс в Python, используя Tkinter и классы, и у меня возникают проблемы с изменением размера определенных строк в сетевом упаковщике.
Код, который я прикрепил, работает, но я хотел бы контролировать ширину строки, чтобы верхние 2 кадра были статическими по размеру, а нижние изменялись вместе с окном. Я пробовал много вещей, ссылаясь на разные решения, но ничего не работает.
# !/usr/bin/python3
# Use Tkinter for python 2, tkinter for python 3
import tkinter as tk
# Main Window
class WinMain(tk.Frame):
def __init__(self, master):
# parameters that you want to send through the Frame class.
tk.Frame.__init__(self, master)
# reference to the master widget, which is the tk window
self.master = master
# with that, we want to then run init_window, which doesn't yet exist
self.init_window()
def init_window(self):
# changing the title of our master widget
self.master.title("GUI Tester")
# Create menu for window
self.createMenu()
# Create Gui for window
self.createGui()
# Update Gui every second after 1 second
self.after(1000, self.upDateGui)
def createMenu(self):
# Initialise drop-down menu
menu = tk.Menu(self.master)
self.master.config(menu=menu)
# Add drop-down menu for Options
options = tk.Menu(menu, tearoff=False)
menu.add_cascade(label="Options", menu=options)
options.add_command(label="Open...", command=self.menuOptionsOpen)
options.add_separator()
options.add_command(label="Close", command=self.menuOptionsClose)
# Add drop-down menu for Help
help = tk.Menu(menu, tearoff=False)
menu.add_cascade(label="Help", menu=help)
help.add_command(label="About...", command=self.menuHelpAbout)
def createGui(self):
# Define GUI using Grid to place widgets
# Size window to its minimum set to 2/3 of the screen resolution
top = self.winfo_toplevel()
screen_width = top.winfo_screenwidth()
screen_height = top.winfo_screenheight()
screen_resolution = str(int(screen_width * 2 / 3)) + 'x' + str(int(screen_height * 2 / 3))
top.geometry(screen_resolution)
top.minsize(int(screen_width * 1 / 3), int(screen_height * 1 / 3))
# ------------------------------------------------
# create all frames
# ------------------------------------------------
self.c = self.master
self.c.frameTop = tk.LabelFrame(self.c, text="Top", width=5, height=5, padx=5, pady=5)
self.c.frameMiddle = tk.LabelFrame(self.c, text="Middle", width=5, height=5, padx=5, pady=5)
self.c.frameBottom = tk.LabelFrame(self.c, text="Bottom", width=5, height=5, padx=5, pady=5)
self.c.frameRight = tk.Frame(self.c, borderwidth=5, relief=tk.GROOVE)
# ------------------------------------------------
# Create widgets for frameTop
# ------------------------------------------------
# Text Box
self.c.frameTop.textBox = tk.Text(self.c.frameTop, borderwidth=3, relief=tk.SUNKEN)
self.c.frameTop.textBox.config(font=("consolas", 12), undo=True, wrap='none')
# Text Box Scroll Bars
self.c.frameTop.textBoxYScroll = tk.Scrollbar(self.c.frameTop, orient=tk.VERTICAL,
command=self.c.frameTop.textBox.yview)
self.c.frameTop.textBox['yscrollcommand'] = self.c.frameTop.textBoxYScroll.set
self.c.frameTop.textBoxXScroll = tk.Scrollbar(self.c.frameTop, orient=tk.HORIZONTAL,
command=self.c.frameTop.textBox.xview)
self.c.frameTop.textBox['xscrollcommand'] = self.c.frameTop.textBoxXScroll.set
# ------------------------------------------------
# Create widgets for frameMiddle
# ------------------------------------------------
# Text Box
self.c.frameMiddle.textBox = tk.Text(self.c.frameMiddle, borderwidth=3, relief=tk.SUNKEN)
self.c.frameMiddle.textBox.config(font=("consolas", 12), undo=True, wrap='none')
# Text Box Scroll Bars
self.c.frameMiddle.textBoxYScroll = tk.Scrollbar(self.c.frameMiddle, orient=tk.VERTICAL,
command=self.c.frameMiddle.textBox.yview)
self.c.frameMiddle.textBox['yscrollcommand'] = self.c.frameMiddle.textBoxYScroll.set
self.c.frameMiddle.textBoxXScroll = tk.Scrollbar(self.c.frameMiddle, orient=tk.HORIZONTAL,
command=self.c.frameMiddle.textBox.xview)
self.c.frameMiddle.textBox['xscrollcommand'] = self.c.frameMiddle.textBoxXScroll.set
# ------------------------------------------------
# Create widgets for frameBottom
# ------------------------------------------------
# Text Box
self.c.frameBottom.textBox = tk.Text(self.c.frameBottom, borderwidth=3, relief=tk.SUNKEN)
self.c.frameBottom.textBox.config(font=("consolas", 12), undo=True, wrap='none')
# Text Box Scroll Bars
self.c.frameBottom.textBoxYScroll = tk.Scrollbar(self.c.frameBottom, orient=tk.VERTICAL,
command=self.c.frameBottom.textBox.yview)
self.c.frameBottom.textBox['yscrollcommand'] = self.c.frameBottom.textBoxYScroll.set
self.c.frameBottom.textBoxXScroll = tk.Scrollbar(self.c.frameBottom, orient=tk.HORIZONTAL,
command=self.c.frameBottom.textBox.xview)
self.c.frameBottom.textBox['xscrollcommand'] = self.c.frameBottom.textBoxXScroll.set
# ------------------------------------------------
# Create widgets for frameRight
# ------------------------------------------------
self.c.frameRight.btnStatus = tk.Button(self.c.frameRight, text='Status Window', command=self.launchWinStatus)
self.c.frameRight.btnSpare1 = tk.Button(self.c.frameRight, text='Send Middle', command=self.btnSpare1)
self.c.frameRight.btnSpare2 = tk.Button(self.c.frameRight, text='Spare 2', command=self.btnSpare2)
# ------------------------------------------------
# ------------------------------------------------
# Layout widgets in frameTop
# ------------------------------------------------
self.c.frameTop.grid_columnconfigure(0, weight=1)
self.c.frameTop.grid_columnconfigure(1, weight=0)
self.c.frameTop.grid_rowconfigure(0, weight=1)
self.c.frameTop.grid_rowconfigure(1, weight=0)
self.c.frameTop.textBox.grid(row=0, column=0, sticky="nsew")
self.c.frameTop.textBoxYScroll.grid(row=0, column=1, sticky="nsew")
self.c.frameTop.textBoxXScroll.grid(row=1, column=0, sticky="nsew")
# ------------------------------------------------
# Layout widgets in frameMiddle
# ------------------------------------------------
self.c.frameMiddle.grid_columnconfigure(0, weight=1)
self.c.frameMiddle.grid_columnconfigure(1, weight=0)
self.c.frameMiddle.grid_rowconfigure(0, weight=1)
self.c.frameMiddle.grid_rowconfigure(1, weight=0)
self.c.frameMiddle.textBox.grid(row=0, column=0, sticky="nsew")
self.c.frameMiddle.textBoxYScroll.grid(row=0, column=1, sticky="nsew")
self.c.frameMiddle.textBoxXScroll.grid(row=1, column=0, sticky="nsew")
# ------------------------------------------------
# Layout widgets in frameBottom
# ------------------------------------------------
self.c.frameBottom.grid_columnconfigure(0, weight=1)
self.c.frameBottom.grid_columnconfigure(1, weight=0)
self.c.frameBottom.grid_rowconfigure(0, weight=1)
self.c.frameBottom.grid_rowconfigure(1, weight=0)
self.c.frameBottom.textBox.grid(row=0, column=0, sticky="nsew")
self.c.frameBottom.textBoxYScroll.grid(row=0, column=1, sticky="nsew")
self.c.frameBottom.textBoxXScroll.grid(row=1, column=0, sticky="nsew")
# ------------------------------------------------
# Layout widgets in frameRight
# ------------------------------------------------
self.c.frameRight.grid_columnconfigure(0, weight=0)
self.c.frameRight.grid_rowconfigure(0, weight=0)
self.c.frameRight.btnStatus.grid(row=0, column=0, sticky="nsew")
self.c.frameRight.btnSpare1.grid(row=2, column=0, sticky="nsew")
self.c.frameRight.btnSpare2.grid(row=3, column=0, sticky="nsew")
# ------------------------------------------------
# Layout frames in top container
# ------------------------------------------------
numOfCols = 2
numOfRows = 6
self.c.grid_columnconfigure(0, weight=1, pad=3)
self.c.grid_columnconfigure(1, weight=0, pad=3)
self.c.grid_rowconfigure(0, weight=1, pad=3)
self.c.grid_rowconfigure(1, weight=1, pad=3)
self.c.grid_rowconfigure(2, weight=1, pad=3)
self.c.grid_rowconfigure(3, weight=1, pad=3)
self.c.grid_rowconfigure(4, weight=1, pad=3)
self.c.grid_rowconfigure(5, weight=1, pad=3)
frameTopRowCol = [0, 0]
frameTopSpan = [2, 1]
frameMiddleRowCol = [frameTopRowCol[0] + frameTopSpan[0],
frameTopRowCol[1]]
frameMiddleSpanRC = [2, 1]
frameBottomRowCol = [frameMiddleRowCol[0] + frameMiddleSpanRC[0],
frameMiddleRowCol[1]]
frameBottomSpanRC = [2, 1]
frameRightRowCol = [frameTopRowCol[0],
frameTopRowCol[1] + frameTopSpan[1]]
frameRightSpanRC = [numOfRows, 1]
self.c.frameTop.grid(row=frameTopRowCol[0], column=frameTopRowCol[1],
rowspan=frameTopSpan[0], columnspan=frameTopSpan[1], sticky="nsew")
self.c.frameMiddle.grid(row=frameMiddleRowCol[0], column=frameMiddleRowCol[1],
rowspan=frameMiddleSpanRC[0], columnspan=frameMiddleSpanRC[1], sticky="nsew")
self.c.frameBottom.grid(row=frameBottomRowCol[0], column=frameBottomRowCol[1],
rowspan=frameBottomSpanRC[0], columnspan=frameBottomSpanRC[1], sticky="nsew")
self.c.frameRight.grid(row=frameRightRowCol[0], column=frameRightRowCol[1],
rowspan=frameRightSpanRC[0], columnspan=frameRightSpanRC[1], sticky="nsew")
# ------------------------------------------------
# Layout top container in window
# ------------------------------------------------
self.grid_columnconfigure(0, weight=1, pad=3)
self.grid_rowconfigure(1, weight=1, pad=3)
# ------------------------------------------------
# Layout window
# ------------------------------------------------
top.grid_columnconfigure(0, weight=1, pad=3)
top.grid_rowconfigure(1, weight=1, pad=3)
def menuOptionsOpen(self):
print("menuOptionsOpen")
def menuOptionsClose(self):
print("menuOptionsClose")
def menuHelpAbout(self):
print("menuHelpAbout")
def launchWinStatus(self):
print ("launchWinStatus")
def btnSpare1(self):
print("btnSpare1")
def btnSpare2(self):
print("btnSpare2")
def upDateGui(self):
print("upDateGui")
# Perform update every x milliseconds
self.after(1000, self.upDateGui)
# Main
def main():
root = tk.Tk()
app = WinMain(root)
root.mainloop()
# Launch Main
if __name__ == '__main__':
main()
У меня есть строка в строке 174, где я могу изменить веса конфигурации строк, и это имеет специфические эффекты, которые я не могу объяснить. Он должен иметь возможность фиксировать высоту некоторых строк, в то время как другим строкам разрешается увеличиваться / уменьшаться с уменьшением окна до минимального размера.