Python 将pygame中的文本格式化为表格

Python 将pygame中的文本格式化为表格,python,text,formatting,pygame,Python,Text,Formatting,Pygame,我需要一些在pygame中格式化表格的帮助。使用ljust、rjust或center修复表时会出现问题。在正常打印输出中使用ljust时,它看起来应该是这样的。我使用screen.blit来显示文本,您还应该看到它确实从同一位置开始。我对pygame非常陌生,wnat开始学习它,因此我首先使用pygame而不是另一个gui模块 (抱歉,我不知道如何显示我提供的图像链接) 当我在pygame中执行相同操作时,它将关闭。 这些是示例列表 ( ('Day', 'MAY 14', 'MAY 15',

我需要一些在pygame中格式化表格的帮助。使用ljust、rjust或center修复表时会出现问题。在正常打印输出中使用ljust时,它看起来应该是这样的。我使用screen.blit来显示文本,您还应该看到它确实从同一位置开始。我对pygame非常陌生,wnat开始学习它,因此我首先使用pygame而不是另一个gui模块

(抱歉,我不知道如何显示我提供的图像链接)

当我在pygame中执行相同操作时,它将关闭。

这些是示例列表

(
('Day', 'MAY 14', 'MAY 15', 'MAY 16', 'MAY 17', 'MAY 18', 'MAY 19', 'MAY 20', 'MAY 21',
 'MAY 22', 'MAY 23', 'MAY 24', 'MAY 25', 'MAY 26', 'MAY 27', 'MAY 28'),

('Description', 'Partly Cloudy', 'Cloudy', 'Thunderstorms', 'Thunderstorms', 'Thunderstorms',
 'Thunderstorms', 'Thunderstorms', 'PM Thunderstorms', 'Cloudy', 'AM Thunderstorms',
 'Isolated Thunderstorms', 'Mostly Sunny', 'Partly Cloudy', 'Partly Cloudy', 'Isolated Thunderstorms'),

('High', '93°', '85°', '80°', '78°', '77°', '79°', '80°', '82°', '84°', '84°', '85°', '87°', '85°',
 '85°', '85°'),

('Low', '69°', '66°', '66°', '67°', '67°', '67°', '67°', '65°', '65°', '65°', '65°', '62°', '64°', '63°', '65°'),

('Precip', '0%', '20%', '80%', '100%', '80%', '80%', '80%', '40%', '20%', '40%', '30%', '20%', '20%', '20%', '30%'),

('Wind', 'SSW 5 mph ', 'SSW 10 mph ', 'S 7 mph ', 'SSE 6 mph ', 'SSE 9 mph ', 'S 9 mph ', 'SSW 10 mph ',
 'SSW 7 mph ', 'N 5 mph ', 'ESE 6 mph ', 'SE 8 mph ', 'WSW 6 mph ', 'SSE 6 mph ', 'SE 7 mph ', 'ESE 7 mph '),

('Humidity', '41%', '66%', '82%', '88%', '89%', '86%', '81%', '76%', '71%', '68%', '56%', '62%',
 '66%', '65%', '68%'),

'92°',

'Feels Like 96°',

'Wake Forest, NC (27587) ',

('Today', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon'))
这是Pygame中使用的代码

width = 20
x = 230
for i in range(8):

    weatherTable = font.render(("{} {} {} {} {} {} {}".format(weather[0][i].ljust(width),
                                                              weather[10][i].ljust(width),
                                                              weather[1][i].ljust(width),
                                                              weather[2][i].ljust(width),
                                                              weather[3][i].ljust(width),
                                                              weather[4][i].ljust(width),
                                                              weather[6][i].ljust(width))),
                               True, constWhite)
    screen.blit(weatherTable, (300, x))
    x = x + 20
以下是常规格式的代码

tests = (
('Day', 'MAY 14', 'MAY 15', 'MAY 16', 'MAY 17', 'MAY 18', 'MAY 19', 'MAY 20', 'MAY 21',
 'MAY 22', 'MAY 23', 'MAY 24', 'MAY 25', 'MAY 26', 'MAY 27', 'MAY 28'),

('Description', 'Partly Cloudy', 'Cloudy', 'Thunderstorms', 'Thunderstorms', 'Thunderstorms',
 'Thunderstorms', 'Thunderstorms', 'PM Thunderstorms', 'Cloudy', 'AM Thunderstorms',
 'Isolated Thunderstorms', 'Mostly Sunny', 'Partly Cloudy', 'Partly Cloudy', 'Isolated Thunderstorms'),

('High', '93°', '85°', '80°', '78°', '77°', '79°', '80°', '82°', '84°', '84°', '85°', '87°', '85°',
 '85°', '85°'),

('Low', '69°', '66°', '66°', '67°', '67°', '67°', '67°', '65°', '65°', '65°', '65°', '62°', '64°', '63°', '65°'),

('Precip', '0%', '20%', '80%', '100%', '80%', '80%', '80%', '40%', '20%', '40%', '30%', '20%', '20%', '20%', '30%'),

('Wind', 'SSW 5 mph ', 'SSW 10 mph ', 'S 7 mph ', 'SSE 6 mph ', 'SSE 9 mph ', 'S 9 mph ', 'SSW 10 mph ',
 'SSW 7 mph ', 'N 5 mph ', 'ESE 6 mph ', 'SE 8 mph ', 'WSW 6 mph ', 'SSE 6 mph ', 'SE 7 mph ', 'ESE 7 mph '),

('Humidity', '41%', '66%', '82%', '88%', '89%', '86%', '81%', '76%', '71%', '68%', '56%', '62%',
 '66%', '65%', '68%'),

'92°',

'Feels Like 96°',

'Wake Forest, NC (27587) ',

('Today', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun', 'Mon'))
   width = 15
   for i in range(8):
    print("{} {} {} {} {} {} {}".format(tests[0][i].ljust(width),
                                        tests[10][i].ljust(width),
                                        tests[1][i].ljust(width),
                                        tests[2][i].ljust(width),
                                        tests[3][i].ljust(width),
                                        tests[4][i].ljust(width),
                                        tests[6][i].ljust(width)))

因此,正如您通过代码和提供的图像所看到的,我无法获得正确的格式,并且我不知道如何将其修复以正确显示。问题是,pygame中的文本是如何生成的,我如何修复pygame中的格式以使其看起来更整洁,谢谢

我相信这个问题是由您最终使用空格字符进行格式化这一事实造成的。正在使用的字体。在第一幅图像中,它看起来像一个,在第二幅图像中,它看起来像一个比例的(其字符的宽度可变)-因此一个简单的修复方法是在pygame代码中使用固定宽度字体。是的,这似乎是个问题,我切换到了固定宽度字体,它起了作用。这将是很好的,有更好的控制,但它工作得很好,只是要找到好看的字体来配合它。我要是早点知道就好了。对于那些思考同样的事情并需要例子的人。。转到和,查看示例。