【问题标题】:Tkinter GUI label from a text file curly brackets来自文本文件大括号的 Tkinter GUI 标签
【发布时间】:2020-11-04 19:48:41
【问题描述】:

我正在尝试在 python 中创建一个待办事项列表作为我的第一个 tkinter 项目。我正在使用包含所有任务的文本文件(我仅使用带有 python 的不同程序添加了该文件,该程序用于为其实现 GUI)。我正在将文件作为列表读入程序,并将该列表用作标签的文本。但是当我运行程序时,它看起来很棒,除了这些烦人的大括号。有谁知道如何摆脱它们。在此先感谢您的帮助。 :) This is what the GUI looks like

And this is the contents of the text file

from tkinter import *
import re

window = Tk()
window.configure(bg="black")
window.title("To Do List")



#Title-----------------------------------------------------------------------------------------------------------------------------------------------------------------------# 

title = Label(window, text="To Do List", font=("Arial", 20, "bold"), fg = "white", bg = "black")
title.grid(column=0, row=0)

#Buttons ----------------------------------------------------------------------------------------------------------------------------------------------------------------#

button_frame = Frame(bg = "black", highlightcolor = "white", highlightthickness=3)
button_frame.grid(row=1,column=0)

button_fonts = ("Arial",10,"bold")

add_task = Button(button_frame, text="Add Task", font=(button_fonts), fg = "white", bg = "black" )
add_task.grid(column=0, row=2)

choose_random = Button(button_frame, text="Choose a Random Task", font=(button_fonts), fg = "white", 
bg = "black" )
choose_random.grid(column=0, row=3)

delete = Button(button_frame, text="Delete", font=(button_fonts), fg = "white", bg = "black" )
delete.grid(column=0, row=4)

delete_all = Button(button_frame, text="Delete All", font=(button_fonts), fg = "white", bg = "black" )
delete_all.grid(column=0, row=5)

close = Button(button_frame, text="Close", font=(button_fonts), fg = "white", bg = "black" )
close.grid(column=0, row=6)


#Tasks ---------------------------------------------------------------------------------------------------------------------------------------------------------------#

tasks_frame = Frame(bg = "black")
tasks_frame.grid(column=1, row=0, rowspan=2)

file = open("Python Tasks.txt","r")
counter = 0
content = file.read()
contlist = content.split("\n")

for i in contlist:
    if i:
        counter += 1
file.close()

##number = Label(window, text=("You have", counter, "uncompleted tasks"), font=(button_fonts))
##number.grid(columns=10,row=0)

file = open("Python Tasks.txt", "r")
tasks = []
for line in file:
    tasks.append(line)



print (tasks)

tasks_lbl = Label(tasks_frame, text=(tasks), font=button_fonts, bg="black", fg="white")
tasks_lbl.grid(column=0, row=0)

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    花括号在那里是因为你给 tkinter 一个列表,当它需要一个字符串时。简单的解决方案是将列表转换为字符串,然后再将其用作标签的文本。

    tasks_lbl = Label(..., text="\n".join(tasks), ...)
    

    tkinter 是 tcl/tk 解释器的一个薄包装器。 Tcl 使用花括号来描述列表中的项目,以便可以将列表转换为字符串,然后再转换回列表。因此,当您传递一个列表时,它会使用自己的逻辑将列表转换为字符串,然后再将其添加到标签中。您希望通过在将列表交给 tcl 之前显式转换列表来避免此步骤。

    这是一个简短的程序,说明了使用带有标签的列表或字符串之间的区别:

    import tkinter as tk
    
    data = ["one two", "three four", "five six"]
    
    root = tk.Tk()
    
    l1 = tk.Label(root, text=data)
    l2 = tk.Label(root, text="\n".join(data))
    
    l1.pack(side="top", fill="x", padx=4, pady=4)
    l2.pack(side="top", fill="x", padx=4, pady=4)
    

    root.mainloop()

    【讨论】:

    • 花括号仅适用于字符串中有空格的项目。所以data = ["one two", "three", "four five"] 会得到{one two} three {four five}
    • @acw1668:你不太正确。大括号和/或反斜杠不仅可以用于空格。
    • 非常感谢!我不敢相信我没有想到这一点。我还在学习,谢谢提醒。 :)
    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2012-01-08
    • 2019-03-10
    • 2018-09-24
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多