如何使用 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()
命令似乎没有任何内容。当我运行程序时,他们仍然将文本集中在我给他们的点上,而不是在该点上正确证明。我找不到解决方案,请帮忙!
I am looking for a way to right justify my string. This is my code:
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()
The .rjust()
commands does not seem to be anything. When I run the program, they still center the text on the point that i gave them, and not right justifying on that point. I cannot find a solution to this, please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您必须对所有 .rjust() 使用相同的宽度,该宽度足以包含列中最长的字符串。例如 14。
其次,您必须使用固定宽度字体,因为使用比例字体时您无法按字符精确对齐。
第三,您应该使用图形类的右对齐函数而不是基于字符串的 .rjust()。看来这个图形类没有这个功能,所以你应该使用 .rjust() 和固定宽度字体,或者你应该使用另一个图形库。
我希望它有帮助。 :)
First, you have to use same width for all of the .rjust() what is big enough to contain the longest string in the column. For example 14.
Second, you have to use fixed width font because with proportional fonts you can't align precisely by characters.
Third, you should use graphics class's right align function instead of the string based .rjust(). It seems this graphics class doesn't have this functionality so you should use .rjust() and fixed width fonts or you should use another graphics library.
I hope it helps. :)