【问题标题】:Tkinter label won't output onto a canvasTkinter 标签不会输出到画布上
【发布时间】:2018-01-19 21:42:32
【问题描述】:

我在其他地方找到了这段代码并对其进行了稍微编辑以满足我的需要,它的目的是将 tkinter 标签以我可以滚动的方式输出到画布上,但是它不会将标签输出到画布上。该代码的目的是为我正在构建的语音控制程序提供一个 GUI,旨在将查询和响应输出到画布上。我对使用 tkinter 很陌生。这是使用 python 3.6.2

vscrollbar = Scrollbar(Window, orient=VERTICAL)
vscrollbar.place(x = 1860, y = 210, height = 850, width = 50)
canvas = Canvas(Window, bd=0, highlightthickness=0,yscrollcommand=vscrollbar.set)
canvas.place(x = 10, y = 210, height = 850, width = 1850)
vscrollbar.config(command=canvas.yview)

    # reset the view
canvas.xview_moveto(0)
canvas.yview_moveto(0)

    # create a frame inside the canvas which will be scrolled with it
interior = Frame(canvas)
interior_id = canvas.create_window(0, 0, window=interior,anchor=NW)
file = open('querys.txt').read().split('\n')
num = 10
print(len(file))
for i in range(len(file)):
    Button(interior, text = file[i]).place()
    print(file[i])
    file2 = list(file[i])
    if file2[0] == '0':
        file2.pop(0)
        file2.pop(0)
        file2[0] = file2[0].upper()
        label = Label(interior_id, text = ''.join(file2), bg = 'blue', wraplength = 1000).place(width = 1000, x = 900, y = num)
        num+label.winfo_height()
    elif file2[0] == '1':
        file2.pop(0)
        file2.pop(0)
        file2[0] = file2[0].upper()
        label = Label(interior_id, text = ''.join(file2), bg = 'blue', wraplength = 1000).place(width = 1000, x = 900, y = num)
        num+label.winfo_height()

【问题讨论】:

  • 请提供minimal reproducible example,而不是一些相关代码。
  • 一方面,标签必须是小部件的子级,而不是画布对象 id 的子级。
  • 所以我必须将位置设置为框架内部而不是内部 ID
  • 使用带有画布的 tkinter 窗口,然后在其上放置标签。

标签: python-3.x tkinter tkinter-canvas


【解决方案1】:
canvas.CreateText()

如果您想设置 字体颜色,请使用 fill="color"

【讨论】:

    猜你喜欢
    • 2022-12-06
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多