【问题标题】:Why does this tkinter frame not surround both buttons为什么这个 tkinter 框架不包围两个按钮
【发布时间】:2020-01-18 22:31:40
【问题描述】:

正在玩 python3 和 tkinter。

我试图在框架中有一个带有两个标签的框架。我想在框架和每个标签周围画一个边框。

因为它是下面的代码显示框架仅在标签 1 周围。标签 2 在 notebook_Frame 下方。 如何确保小部件位于特定框架内?

import tkinter as tk

def main():
    mainWinTk = tk.Tk()
    mainWinTk.title("Setup Server")
    mainWinTk.columnconfigure(0, weight=1)
    mainWinTk.rowconfigure(0, weight=1)
    mainWinTk.rowconfigure(1, weight=1)

    notebook_Frame = tk.Frame(mainWinTk, highlightbackground="green", highlightthickness=3, borderwidth=2, relief='ridge').grid(column=0, row=0, sticky=tk.W+tk.E+tk.S+tk.N)    

    tk.Label(notebook_Frame, text="Label 1", highlightbackground="red", highlightthickness=3).grid(column=0, row=0)
    tk.Label(notebook_Frame, text="Label 2", highlightbackground="red", highlightthickness=3).grid(column=0, row=1)

    mainWinTk.mainloop()
if __name__ == "__main__":
    main()  

【问题讨论】:

  • 因为notebook_FrameNone

标签: python-3.x tkinter


【解决方案1】:

刚刚找到原因,是的,Bryan Oakly 是正确的。

对 tk.Frame() 的调用将返回一个帧。 但是,当您执行 tk.Frame().grid() 时,网格的返回为无。 你必须这样做:

notebook_Frame = tk.Frame(mainWinTk, highlightbackground="green", highlightthickness=3, borderwidth=2, relief='ridge')
notebook_Frame.grid(column=0, row=0, sticky=tk.W+tk.E+tk.S+tk.N)

作为两行单独的代码。

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多