如何在图形窗口中使用Python右对齐文本?
我想要把我的字符串右对齐。这是我的代码:
from graphics import*
def main():
win = GraphWin("Simple Editor", 600, 400)
win.setCoords(0,0,60,40)
#Text - Filename
s = "File Name:"
s=s.rjust(10)
text1 = Text(Point(10, 35), s)
text1.draw(win)
#Text - Keyword
s1 = "Keyword:"
s1=s1.rjust(8)
text2 = Text(Point(10, 28), s1)
text2.draw(win)
#Text - Replace with
s2 = "Replace with:"
s2=s2.rjust(10)
text2 = Text(Point(10, 21), s2)
text2.draw(win)`
main()
我发现 .rjust()
这个命令似乎没有什么作用。当我运行程序时,文本还是在我指定的点上居中,而不是在那个点右对齐。我找不到解决办法,请帮帮我!
1 个回答
1
首先,你需要给所有的 .rjust() 设置相同的宽度,这个宽度要足够大,能够容纳这一列中最长的字符串。比如说,设置为14。
其次,你要使用等宽字体,因为用比例字体的话,字符对齐就不那么精确了。
第三,你应该使用图形类的右对齐功能,而不是基于字符串的 .rjust()。看起来这个图形类没有这个功能,所以你要么使用 .rjust() 和等宽字体,要么换一个图形库。
希望这些能帮到你。:)