微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

字符串格式在tkinter中不起作用

如何解决字符串格式在tkinter中不起作用

我一直在尝试学习tkinter并创建了一些东西来发布一堆函数的结果,并且在终端中字符串格式有效,但是在gui中,字符串格式根本不起作用。我对为什么感到困惑?

代码如下:

from tkinter import *
import ForFeesCovered

root = Tk()
root.title("Staff Fee Calculator")
root.geometry("375x400")

myLabel = Label(root,text="Staff Fee Calculator")

e = Entry(root,width=50,borderwidth=5)


def output():
    input_file = e.get()
    f = ForFeesCovered.readfile(input_file)
    file = ForFeesCovered.readfile(input_file)
    staff = ForFeesCovered.getnamesofstaff(f)
    staff.sort(reverse=False)
    dic = ForFeesCovered.sort_dic(staff)

    line_skip = 1
    for lines in file:
        line = lines.strip().split(",")
        if line_skip != 1:
        total = float("        
        {:.2f}".format(ForFeesCovered.getfeesforline(line)))
            name = ForFeesCovered.get_name_of_staff(line,staff)
            dic = ForFeesCovered.populating_dic(dic,name,total)
        else:
            line_skip += 1

    string_dic = ""
    result_file = open("result.txt","w+")
    for key in dic:
        result_file.write("{} : {}\n".format(key,dic[key]))
        string_dic = string_dic + "{:30} : {:>30}\n".format(key,dic[key])
        print(string_dic)
    result_file.close()

    output_dic = Label(root,text=string_dic,justify=LEFT)
    output_dic.grid(row=2,column=0,pady=20)

submit = Button(root,text="Submit",command=output)

myLabel.grid(row=0,column=0)
e.grid(row=1,)
submit.grid(row=1,column=2)


root.mainloop()

Image

解决方法

终端使用的是固定宽度的字体,GUI使用的是可变宽度的字体。

如果您尝试将空格对齐,则需要使用固定宽度的字体。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。