将pygame中的文本格式化为选项卡

2024-09-27 23:18:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

https://imgur.com/a/k3dpVT9

When I do the same in pygame, it is off. 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中使用的代码。

^{pr2}$

这是正常格式的代码。

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中的格式使其看起来更整洁,谢谢!


Tags:
testswidthpygamemayssecloudyesese

热门问题