问题描述
我一直在尝试学习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()
解决方法
终端使用的是固定宽度的字体,GUI使用的是可变宽度的字体。
如果您尝试将空格对齐,则需要使用固定宽度的字体。