首页 > 解决方案 > Python tkinter 文本到边界问题

问题描述

我正在用 tkinter 在 Python 中编写一个小 GUI 程序。我现在遇到了文本和标签小部件周围的垂直对齐和间距问题。有两种情况。

案例一

文本小部件与其边框/突出显示之间的垂直间距,如前 3 个文本小部件所见。即使我使用pady,它也会以不均匀的方式变大

案例二

从标签到文本小部件的垂直对齐也关闭了。在这里我完全不知道如何影响它。

我在 (manjaro) linux 上运行它。

最小示例的屏幕截图

import tkinter as tk


class GuiTest():
    def __init__(self, root):
        self.root = root
        root.wm_title("GUI Test")
        root.geometry("800x600")
        root.attributes('-type', 'dialog')

        baseinfo = tk.Frame(root, padx=10, pady=10)
        baseinfo.grid(row = 4, column=4, sticky='EW')

        self.loc = tk.Text(baseinfo, width=10, height=1, bd=0, highlightthickness=1, font=("Courier", "14"))
        self.loc.grid(row=1, column=1)

        self.year = tk.Text(baseinfo, width=15, height=1, bd=1, highlightthickness=1, font=("Courier", "14"))
        self.year.grid(row=1, column=2)


        self.typ = tk.Text(baseinfo, width=10, height=1, bd=0, highlightthickness=1, font=("Courier", "14"), pady='10')
        self.typ.grid(row=2, column=1)


        tk.Label(baseinfo, text="Label").grid(row=3, column=3)

        self.col = tk.Text(baseinfo, width=15, height=1, bd=0, highlightthickness=0, font=("Courier", "11"))
        self.col.grid(row=3, column=4)


        self.loc.insert('end', "Test")
        self.year.insert('end', "another Test")
        self.col.insert('end', "Color")
        self.typ.insert('end', "More Text")
        self.col.configure(background='red')


if __name__ == '__main__':
    root = tk.Tk()
    app = GuiTest(root)
    root.mainloop()

如果代码中有任何大的 NoNos,请告诉我们,我仍然是 tkinter 的初学者。

谢谢

标签: pythontkinter

解决方案


正如@stovfl 指出的那样,这是我的系统问题,而不是代码问题。它在reply.it上运行良好


推荐阅读