【问题标题】:tkinter display new frame from menutkinter 从菜单中显示新框架
【发布时间】:2018-11-15 15:32:51
【问题描述】:

我正在尝试用 python 和 tkinter 编写我的第一个脚本。

当单击菜单栏按钮时,我阻止显示来自函数 (def onDisplay) 的新框架,但没有附加任何内容。 没有错误显示:-(

新框架由文本文件中的动态复选框组成: txt 文件:

item1
item2
...
item100

GUI 屏幕:

这是我的代码:

from tkinter import Tk, Frame, Menu, Checkbutton, Text, TOP, BOTH, X, N, LEFT, BooleanVar
from tkinter.ttk import Frame, Label, Entry
import glob

class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()
        #self.display_srv()

    def initUI(self):

        self.master.title("Submenu")

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)

        submenu = Menu(fileMenu)
        submenu.add_command(label="lst1", command=self.onDisplay)
        submenu.add_command(label="lst2")
        submenu.add_command(label="lst3")
        fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)

        fileMenu.add_separator()

        fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)

    #The frame i tried to display
    def onDisplay(self):
        self.master.title("display it")
        self.pack(fill=BOTH, expand=True)
        frame1 = Frame(self)
        frame1.pack(fill=X)
        path = '/root/liste/*.txt'
        files=glob.glob(path)
        count = 0
        for file in files:
            with open(file, 'r') as lst_file:
                for item in lst_file:
                    # Need to split all item by 10
                    Checkbutton(self, text=item.rstrip()).grid(row=count//10, column=count%10)
                    count += 1


    def onClick(self):
        if self.var.get() == True:
            self.master.title("Checkbutton")
        else:
            self.master.title("")

    def onExit(self):

        self.quit()


def main():

    root = Tk()
    root.geometry("850x550+300+300")
    app = Example()
    root.mainloop()


if __name__ == '__main__':

非常感谢您的帮助

问候,

【问题讨论】:

  • 你做了什么来调试这个?您是否已验证 files=glob.glob(path) 正在按照您的想法进行操作?
  • 是的,我在独立脚本中使用了这部分代码,没有菜单栏。
  • 该代码将.pack(....grid(... 混合使用,将永远无法工作。参观并重新阅读它是如何工作的tkinterbook
  • 好的,谢谢你的链接 stovfl。我用 .pack 替换:Checkbutton(self, text=item.rstrip()).pack 但问题是一样的.. :-/

标签: python checkbox tkinter dynamic frame


【解决方案1】:

主要问题是您将gridpack 混合在同一个父容器中。您在frame1 上调用pack,但您在复选按钮上调用grid,并且它们都有self 的主人或父母。

这是行不通的,因为gridpack 都会尝试根据自己的规则调整容器的大小,触发另一个根据自己的规则重新配置,以此类推,直到时间结束。

因此,只需将检查按钮上的 .grid(...) 调用更改为 .pack(...) 即可解决该问题。

我的猜测是您打算将复选按钮放在frame1 中。如果是这种情况,您需要将frame1 指定为复选按钮的主控。为了便于阅读和调试,我还建议将grid 的调用放在单独的行上。有了它,您可以继续使用grid 作为复选按钮,并使用pack 进行其他所有操作。

cb = Checkbutton(frame1, text=item.rstrip())
cb.grid(row=count//10, column=count%10)

【讨论】:

  • 非常感谢布莱恩,我现在明白了。我从几天开始就开始使用 python .. :)。我的代码遇到了另一个问题。我今天或明天将在 Stack 上创建一个新主题。如果可以的话请看一下。我搜索如何获取每个选定项目的值。
【解决方案2】:

我发布了正确的代码:

from tkinter import Tk, Frame, Menu, Checkbutton, Text, TOP, BOTH, X, N, LEFT, BooleanVar
from tkinter.ttk import Frame, Label, Entry
import glob

class Example(Frame):

    def __init__(self):
        super().__init__()

        self.initUI()
        #self.display_srv()

    def initUI(self):

        self.master.title("Submenu")

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)

        submenu = Menu(fileMenu)
        submenu.add_command(label="lst1", command=self.onDisplay)
        submenu.add_command(label="lst2")
        submenu.add_command(label="lst3")
        fileMenu.add_cascade(label='Listing', menu=submenu, underline=0)

        fileMenu.add_separator()

        fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)
    def onDisplay(self):
        self.master.title("display it")
        self.pack(fill=BOTH, expand=True)
        frame1 = Frame(self)
        frame1.pack(fill=X)
        path = '/root/liste/*.txt'
        files=glob.glob(path)
        count = 0
        for file in files:
            with open(file, 'r') as lst_file:
                for item in lst_file:
                    cb = Checkbutton(frame1, text=item.rstrip())
                    cb.grid(row=count//10, column=count%10)
                    count += 1

    def onClick(self):
        if self.var.get() == True:
            self.master.title("Checkbutton")
        else:
            self.master.title("")

    def onExit(self):

        self.quit()

def main():

    root = Tk()
    root.geometry("850x550+300+300")
    app = Example()
    root.mainloop()

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2020-09-15
    相关资源
    最近更新 更多