Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的等分词_Python_String_List_Listbox_Format - Fatal编程技术网

Python中的等分词

Python中的等分词,python,string,list,listbox,format,Python,String,List,Listbox,Format,我想做的是分开文本块,这样我在每行有两个文本块,在不同的行中,它们从同一点开始。我使用它的目的是开发一个小型图书管理器程序,供我自己使用,所以它应该是这样的: Book Title Here Author Name Here Little longer title here Author Name Here shorter here Author Name Here 我试着使用空格来使用.lj

我想做的是分开文本块,这样我在每行有两个文本块,在不同的行中,它们从同一点开始。我使用它的目的是开发一个小型图书管理器程序,供我自己使用,所以它应该是这样的:

Book Title Here                    Author Name Here
Little longer title here           Author Name Here
shorter here                       Author Name Here
我试着使用空格来使用
.ljust()
.rjust()
,但这对我来说并没有真正起作用:无论出于何种原因,空格都不均匀,我最终没有将标题堆叠在一起,而是几乎没有分开


我正在使用Tkinter构建GUI,每一行都应该是列表框中的一个项目。

如果您使用的是固定宽度字体,那么
“{:40}{}”。格式(“此处书名”,“此处作者姓名”
是您的朋友。(将40更改为您希望为第一部分分配的空间数量。)

如果您使用的是可变宽度字体,那么您需要使用Tkinter的排列方式,这可能归结为将每行的两部分放在各自的部分中

例如,您可以按照以下方式进行操作:

Label(master, text="Book Title Here").grid(row=0, sticky=W)
Label(master, text="Author Name Here").grid(row=0, column=1, sticky=W)

Label(master, text="Little longer title here").grid(row=1, sticky=W)
Label(master, text="Author Name Here").grid(row=1, column=1, sticky=W)

如果您使用的是固定宽度字体,则
“{:40}{}”。格式(“此处书名”,“此处作者姓名”
是您的朋友。(将40更改为您希望为第一部分分配的空格数。)

如果您使用的是可变宽度字体,那么您需要使用Tkinter的排列方式,这可能归结为将每行的两部分放在各自的部分中

例如,您可以按照以下方式进行操作:

Label(master, text="Book Title Here").grid(row=0, sticky=W)
Label(master, text="Author Name Here").grid(row=0, column=1, sticky=W)

Label(master, text="Little longer title here").grid(row=1, sticky=W)
Label(master, text="Author Name Here").grid(row=1, column=1, sticky=W)

我建议使用,以下是设置:

bookdict = {
  'Little longer title here': 'Author Name Here',
  'Book Title Here': 'Another Author Name Here',
  'shorter here': 'Diff Name Here'}
bookwidth = max(map(len, bookdict.keys()))
authorwidth = max(map(len, bookdict.values()))
格式
迷你语言 迷你语言的这种用法:

template = '{{0:<{bw}}} {{1:>{aw}}}'.format(bw=bookwidth, aw=authorwidth)
for book, author in bookdict.items():
    print( template.format(book, author) )
为了打破这种情况,双大括号将保留在第一种格式上,并减少为单大括号,单大括号将成为计算的最大镜头宽度,例如:

'{0:<30} {1:>20}'
印刷品:

Little longer title here         Author Name Here
Book Title Here          Another Author Name Here
shorter here                       Diff Name Here
Little longer title here         Author Name Here
shorter here                       Diff Name Here
Book Title Here          Another Author Name Here

我建议使用,以下是设置:

bookdict = {
  'Little longer title here': 'Author Name Here',
  'Book Title Here': 'Another Author Name Here',
  'shorter here': 'Diff Name Here'}
bookwidth = max(map(len, bookdict.keys()))
authorwidth = max(map(len, bookdict.values()))
格式
迷你语言 迷你语言的这种用法:

template = '{{0:<{bw}}} {{1:>{aw}}}'.format(bw=bookwidth, aw=authorwidth)
for book, author in bookdict.items():
    print( template.format(book, author) )
为了打破这种情况,双大括号将保留在第一种格式上,并减少为单大括号,单大括号将成为计算的最大镜头宽度,例如:

'{0:<30} {1:>20}'
印刷品:

Little longer title here         Author Name Here
Book Title Here          Another Author Name Here
shorter here                       Diff Name Here
Little longer title here         Author Name Here
shorter here                       Diff Name Here
Book Title Here          Another Author Name Here

我试过了,打印出来的时候效果不错,但是当我把它插入列表框的时候,我不知道为什么,我猜这与tkinter默认使用的字体有关。确实是这样,因为我更改了字体,你的解决方案对我很好。谢谢。我试过了,打印出来的时候效果不错,但是当我把它插入列表框,我真的不知道为什么,我猜这与tkinter默认使用的字体有关确实如此,因为我更改了字体,您的解决方案对我很有效。谢谢。