为什么在下面这个文本编辑器程序的 Tkinter grid_rowconfigure 和 grid_columnconfigure 方法中使用 index = 0?

问题描述 投票:0回答:1

下面是文本编辑器程序的代码。据我了解, grid_rowconfigure() 和 grid_columnconfigure() 方法与动态调整大小有关 Tkinter 网格,以便网格响应窗口大小的调整,但我不这样做 了解为什么两种方法都选择索引 0。 此外,当将 text_area 小部件放在网格上时,没有行或列索引
指定但它正确定位在网格上。

有人可以帮我理解为什么这样写吗?

文本编辑器程序:

import os
from tkinter import *
from tkinter import filedialog, colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *

def change_color():
    color = colorchooser.askcolor(title="pick a color...or else")
    text_area.config(fg=color[1])


def change_font(*args):
    text_area.config(font=(font_name.get(), size_box.get()))


def new_file():
    window.title("Untitled")
    text_area.delete(1.0, END)




def open_file():
    file = askopenfilename(defaultextension=".txt", file=[("All Files", "*.*"), ("Text      Documents,""*.txt")])



    try:
        window.title(os.path.basename(file))
        text_area.delete(1.0, END)

        file = open(file, "r")
        text_area.insert(1.0, file.read())

    except Exception:
        print("couldn't read file")

    finally:
        file.close()


def save_file():
    file = filedialog.asksaveasfilename(initialfile="untitled.txt",
                                        defaultextension=".txt",
                                        filetypes=[("All Files", "*.*"),
                                                   ("Text Documents", "*.txt")])

    if file is None:
        return

    else:
        try:
            window.title(os.path.basename(file))
            file = open(file, "w")
            file.write(text_area.get(1.0, END))


        except Exception:
            print("couldn't save file")

        finally:
            file.close()



def cut():
    text_area.event_generate("<<Cut>>")

def copy():
    text_area.event_generate("<<Copy>>")

def paste():
    text_area.event_generate("<<Paste>>")


def about():
    showinfo("About this program", "This is a program written by you")


def quit():
    window.destroy()

window = Tk()

window.title("Text Editor Program")
file = None


window_width = 500
window_height = 500


screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()


x = int((screen_width/2) - (window_width/2))
y = int((screen_height/2) - (window_height/2))


window.geometry("{}x{}+{}+{}".format(window_width, window_height, x, y))



font_name = StringVar(window)
font_name.set("Arial")




font_size = StringVar(window)
font_size.set("25")



text_area = Text(window, font=(font_name.get(), font_size.get()))


scroll_bar = Scrollbar(text_area)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
scroll_bar.pack(side=RIGHT, fill=Y)
text_area.config(yscrollcommand=scroll_bar.set)


frame = Frame(window)
frame.grid()

color_button = Button(frame, text="color", command=change_color)        
color_button.grid(row=0, column=0)                                      

                                                             
font_box = OptionMenu(frame, font_name, *font.families(), command=change_font)
font_box.grid(row=0,column=1)
                                   

size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size,command=change_font)
size_box.grid(row=0,column=2)



menu_bar = Menu(window)
window.config(menu=menu_bar)

file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)


file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=quit)


edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=cut)
edit_menu.add_command(label="Copy", command=copy)
edit_menu.add_command(label="Paste", command=paste)

help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=about)


window.mainloop()

我尝试删除 window.grid_rowconfigure() 和
window.grid_columnconfigure() 方法。
我还更改了frame.grid() 的参数(参见附图),因为我不明白
为什么frame.grid()不需要任何索引规范。 进行这些更改时,生成了以下附加图像。

here are various GUI image outputs.  The associated syntax for each image is what affects the various appearance but I don't fully understand how these changes in syntax make these changes in appearance.

tkinter tkinter-layout
1个回答
0
投票

如果没有指定行号或列号,tkinter 将自动计算行号和列号。在这种情况下,文本小部件是添加到窗口的第一个内容,因此默认情况下它会转到第 0 行、第 0 列。

对该行和列调用

row_configure
column_configure
是为了保证窗口中任何分配的空间都分配给文本小部件,以便它在 UI 中占用尽可能多的空间。

© www.soinside.com 2019 - 2024. All rights reserved.