简体   繁体   English

tkinter 小部件未出现

[英]tkinter widget doesn't appear

I'm trying to make a very simple tkinter app, but I can't seem to figure out why one of my widgets isn't appearing.我正在尝试制作一个非常简单的 tkinter 应用程序,但我似乎无法弄清楚为什么我的一个小部件没有出现。 The below code should render the string '30' in a row above the button-label pairs, but it only shows the button-label pairs and no additional string.下面的代码应该在按钮-标签对上方的一行中呈现字符串“30”,但它只显示按钮-标签对,没有额外的字符串。

from tkinter import *
from tkinter import ttk

data = {
    'projects': {
      'project': {
           'name': 'Project',
           'starttime': 0
    },
       'another': {
           'name': 'Another',
           'starttime': 0
       }
    },
    'clock': {
        'clockedin?': False,
        'time': 30
    }
}

def makebutton():
    row = 1
    for item in data['projects']:
        ttk.Button(frm, text=data['projects'][item]['name']).grid(column=0,row=row)
        ttk.Label(frm, text=data['projects'][item]['starttime']).grid(column=1, row=row)
        row = row + 1

root = Tk()

frm = ttk.Frame(root, padding=10)
frm.grid()

ttk.Label(frm, text=data['clock']['time']).grid(column=0, row=2) # should say '30'

makebutton()

root.mainloop()

If I move the '30' label line below makebutton() , it displays '30', but over the bottom button.如果我将'30' label 行makebutton()下方,它会显示'30',但在底部按钮上方。

What am I misunderstanding here?我在这里有什么误解?

You are putting the label on row 2, and then in makebutton you're putting widgets in rows 1 and 2. Since makebutton is called after you create the label, the second button it creates sits on top of the label.您将 label 放在第 2 行,然后在makebutton小部件放在第 1 行和第 2 行。由于在创建makebutton后调用了 makebutton,因此它创建的第二个按钮位于 ZD304BA20E96D8088EEABAC851 的顶部。

You should either move the label to row 3, or initialize row to 0 at the top of makebutton .您应该将 label 移动到第 3 行,或者在makebutton的顶部将row初始化为0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM