[英]visual studio code - python - tkinter
import tkinter
canvas = tkinter.Canvas()
canvas.pack()
tkinter.mainloop()
canvas.create_text(150, 100, text = "HELLO")
Sorry for my English.对不起我的英语不好。 Start in Python.从 Python 开始。 Canvas is on screen, but NO text What is wrong? Canvas 出现在屏幕上,但没有文字 怎么了? Thanks.谢谢。
Your code isn't executed.你的代码没有被执行。 You call tkinter.mainloop(), which basically stops execution of all code after it.你调用 tkinter.mainloop(),它基本上停止执行它之后的所有代码。 Here, replace it with this instead:在这里,将其替换为:
import tkinter
canvas = tkinter.Canvas()
canvas.pack()
canvas.create_text(150, 100, text = "HELLO")
tkinter.mainloop()
Make sure "tkinter.mainloop()" comes after the code, so it executes.确保“tkinter.mainloop()”出现在代码之后,以便它执行。
I hope this helps.我希望这有帮助。 Happy coding!快乐编码!
Simply because you use .mainloop()
before creating your text.仅仅因为您在创建文本之前使用.mainloop()
。 Put your create_text
line just under the one where you create the canvas and it works.将您的create_text
行放在您创建 canvas 的行的下方,它可以工作。
I guess this is what You were looking for:我想这就是你要找的:
from tkinter import Tk, Canvas
root = Tk()
canvas = Canvas(root)
canvas.pack()
canvas.create_text(150, 100, text='Hello')
root.mainloop()
Tho looking at Your code I would say You have to take a look at some basic python tutorials as well as some tkinter tutorials.看着你的代码,我会说你必须看看一些基本的 python 教程以及一些 tkinter 教程。 You really need them!你真的需要他们! For example this about tkinter例如关于 tkinter
The problem occurs because you are using root.mainloop
this means that everything before it runs but everything after the root.mainloop
runs when you close the program.出现问题是因为您使用的是root.mainloop
这意味着它运行之前的所有内容,但root.mainloop
之后的所有内容都会在您关闭程序时运行。
So if you want to see the text on the canvas do this:因此,如果您想查看 canvas 上的文字,请执行以下操作:
from tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()
canvas.create_text(150, 100, text = "HELLO")
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.