【问题标题】:Tkinter Label wont appear inside LabelframeTkinter Label 不会出现在 Labelframe 中
【发布时间】:2021-06-22 15:01:53
【问题描述】:

我创建了一个 GUI 来接受 2 个 JSON 文件并进行比较。如下代码所示,我的 Labelframe(policyframe) 是在用于创建滚动条的 second_frame 内。

运行代码时,我成功地分别从文件 1 和 2 中看到包含“名称 1”和“名称 2”的标签。但是,在创建标签以显示第二个文件的描述后,会弹出一个显示空白屏幕的新窗口。

在不同的试验和错误中,这些是我的观察结果:

  • 我尝试显示文件 1 的其他键,也能看到它们。
  • 尝试显示两个 JSON 文件的“名称键”并成功。
  • 除了“name_c”之外,无法看到文件 2 的任何其他键

例如还添加了json数据。

from typing import Counter
from file import Root
from io import SEEK_CUR
from tkinter import *
from tkinter import ttk
from tkinter import filedialog

import json

class Main:
    def __init__(gui, master):
        label1 = Label(second_frame, text = "Test GUI")
        label1.pack()
        button_a = Button(second_frame, text="Choose Json File1", command=gui.file_a)
        button_a.pack()
        button_c = Button(second_frame, text="Choose Json File2", command=gui.file_c)
        button_c.pack()

        button_compare = Button(second_frame, text="Compare Json", command=gui.compare).pack()

    def file_a(gui) :
        gui.filename1 = filedialog.askopenfilename(initialdir = "/", title = "Select a file", filetype = (("json", "*.json"), ("All Files", "*.*")))
        
    def file_c(gui) :
        gui.filename2 = filedialog.askopenfilename(initialdir = "/", title = "Select a file", filetype = (("json", "*.json"), ("All Files", "*.*")))
    
    def compare(gui) :
        Label(second_frame, text = "Starting to Compare").pack()

        with open(gui.filename1) as af:
            data_a = json.load(af)

        with open(gui.filename2) as cf:
            data_c = json.load(cf)
        
        count = 0

        for f1 in data_a['policies'] :
            name_a = f1['displayName']
           
            for f2 in data_c['rules'] :
                name_c = f2['properties']['displayName']
                description_c = f2['properties']['description']

                if name_a == name_c:
                    
                    policy_frame = LabelFrame(second_frame, text='Comparing Json Files', padx=5, pady=5)
                    policy_frame.pack()

                    Label(policy_frame, text=f'Name 1 : {name_a}').pack()
                    
                    Label(policy_frame, text=f'Name 2 : {name_c}').pack()
                    #Label(policy_frame, text=f'Description 2 : {description_c}').pack()
               
                else :
                    count = count + 1
                    
        print(count)                

root = Tk()

root.title("Test GUI")
root.geometry("500x400")

main_frame = Frame(root)
main_frame.pack(fill=BOTH, expand=1)

my_canvas = Canvas(main_frame)
my_canvas.pack (side = LEFT, fill = BOTH, expand=1)

my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)

my_canvas.configure(yscrollcommand = my_scrollbar.set)
my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))

second_frame = Frame(my_canvas)

my_canvas.create_window((0,0), window=second_frame, anchor="nw")

main = Main(root)

root.mainloop()

Json 文件 1

{
  "policies": [
    {
      "displayName": "Name 1",
      "status": {
        "cause": abc,
        "code": "python",
        "description": Test 1
      },
    }
  ]
}

Json 文件 1

{
  "rules": [
    {
      "displayName": "File 2 Name 1",
      "description": "Description of Key 1"
    }
  ]
}

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    实际上正在显示数据,只是labelFrame 的大小正在改变,这就是我看到空白屏幕的原因。

    只需将Label打包到西边,数据就可见了。

    labelname.pack(padx=5, pady=5, anchor = "w"

    这解决了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多