【问题标题】:Why is not presented the custoned widget为什么不显示自定义小部件
【发布时间】:2017-11-06 16:42:32
【问题描述】:

我想展示左侧自定义小部件 3 次。左、中、右。

我试图遵循并混合我找到的一些示例,但我只展示了一个小部件。

我添加了 colout 以获得一些线索,但我无法找到我做错了什么。

这是代码:

import sys
import tkinter as tk
#import tkinter.ttk, Font, Label, Frame as tk
#import tkinter.ttk as tk

#https://stackoverflow.com/questions/27614037/python-3-tkinter-create-text-widget-covering-100-width-with-grid?rq=1
# https://stackoverflow.com/questions/7591294/how-to-create-a-self-resizing-grid-of-buttons-in-tkinter
# http://effbot.org/tkinterbook/grid.htm

# 02. Pruebo cambiando llamada de funcion de clase a metodo de instancia  

class   Cuadro(tk.Frame):
    def __init__(self, parent, *args, **kargs):

        # super().__init__(root)
        tk.Frame.__init__(self, parent, *args, **kargs)

        #Create & Configure frame 

        frame = tk.Frame(parent)       
        frame.grid(row=0, column=0, sticky="NSEW")



        #Create a 3x3 (rows x columns) grid of labels inside the frame
        for row_index in range(3):
        #tk.Grid.rowconfigure(frame, row_index, weight=1)               # version 02
            frame.rowconfigure(row_index, weight=1)                     # version 02
            for col_index in range(3):
                #tk.Grid.columnconfigure(frame, col_index, weight=1)    # version 02
                frame.columnconfigure(col_index, weight=1)              # version 02
                lbl = tk.Label(frame, text = str((row_index *3) + col_index + 1)) #create a button inside frame 
                #lbl.grid(row=row_index, column=col_index, sticky=N+S+E+W)
                lbl.grid(row=row_index, column=col_index, sticky="NSEW")



#Create & Configure root
class   Application(tk.Frame):
    def __init__(self, parent):

        tk.Grid.rowconfigure   (parent, 0, weight=1)
        tk.Grid.columnconfigure(parent, 0, weight=1)
        tk.Grid.columnconfigure(parent, 1, weight=1)
        tk.Grid.columnconfigure(parent, 2, weight=1)
        self.cuadro1 = Cuadro(parent)
        self.cuadro1.config(background="red")
        self.cuadro1.grid(row=0, column=0, sticky="NSEW")     
        self.cuadro2 = Cuadro(parent)
        self.cuadro2.config(bg="green")
        self.cuadro2.grid(row=0, column=1, sticky="NSEW")
        self.cuadro3 = Cuadro(parent)
        self.cuadro3.config(bg="blue")
        self.cuadro3.grid(row=0, column=2, sticky="NSEW")


def main():
    root = tk.Tk()
    root.title(sys.argv[0])    # version 02

    myapp = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()

结果如下:

!http://imgur.com/XROp68R

怎么了?

【问题讨论】:

  • 4 次还是 3 次?你说左分右。那只有 3 个。
  • 对不起!提平错误。 :-(。我已经修好了。

标签: tkinter


【解决方案1】:

您的三个 Cuadro 对象根本没有内容 - 它们是有效的框架,因为您从 tk.Frame 继承并调用了超类 __init__(),但您对它们完全没有做任何事情。相反,每个人都创建了一个完全独立的 Frame,不连接到 Cuadro 本身,并将其放置在父视图中的 row=0,column=0 处。这些中的每一个都覆盖了前一个,以及最初放置在 row=0、column=0 的红色 Cuadro,因此您最终得到了 3x3 网格的单个可见副本,并且没有红色区域。

您对 Application 犯了同样的错误 - 它是一个有效的 Frame,但您没有添加任何内容,而是将每个 Cuadro 创建为 Application 的父级(Tk 根窗口)的直接子级。

拥有只创建像这样的小部件的类是完全有效的,但如果您不打算将它们用作小部件本身,它们不应该派生自 Tk 小部件类。

【讨论】:

  • 正如你所指出的,我已经在 Cuadro 和 Application 类中将依赖关系从 parent 更改为 self,并在 Application 类中添加了 init 框架现在显示了三个 Cuadro,但不调整它们的大小,也不接受背景颜色。我继续做错什么或我缺乏什么?这是新结果:!imgur.com/QfoF7cQ 调整大小时:!imgur.com/8FavoIL 抱歉。我不知道如何添加新代码。 :-(
  • 我想我已经找到了解决方案,按照你的想法,插入` tk.Grid.rowconfigure(root, 0, weight=1) tk.Grid.columnconfigure(root, 0, weight=1) ` 之后 ` root = tk.Tk()`
  • 这种写法很奇怪,应该是root.rowconfigure(0, weight=1)
  • 我注意到了这一点!您的 cmets 非常有帮助!
猜你喜欢
  • 1970-01-01
  • 2021-10-22
  • 2021-11-18
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
  • 2015-11-19
相关资源
最近更新 更多