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

时间:2018-05-16 13:11:27

标签: python text formatting pygame

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

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

https://imgur.com/a/k3dpVT9

当我在pygame中执行相同操作时,它已关闭。 https://i.stack.imgur.com/d0vi3.png

这些是样本列表。

(
('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中修复格式以使其看起来更整洁,谢谢!

0 个答案:

没有答案