Python 3.5:打印画布文本 [英] Python 3.5: Print Canvas Text

查看:25
本文介绍了Python 3.5:打印画布文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能与我分享如何打印添加到 Canvas 对象的文本小部件的文本?在下面的代码中,我希望系统在将鼠标放在文本上时返回hello"的值,但是,结果却给了我1".不知道为什么.有人可以帮我吗?

非常感谢!!!

导入tkinter从 tkinter 导入 *def show_text(event):打印(画布.文本)主 = tkinter.Tk()画布 = tkinter.Canvas(主,宽度 = 200,高度 = 100)画布.pack()canvas.bind('',show_text)canvas.text = canvas.create_text(20, 30, text="你好")主循环()

解决方案

根据

跟进(请参阅

Could anyone share with me how to print the text of the text widget added to a Canvas object? In the code below, I want the system return the value of "hello" when mouse on the text, however, it turns out giving me "1". Don't know why. Could anyone help me?

Many many thanks!!!

import tkinter
from tkinter import *


def show_text(event):
    print (canvas.text)

master = tkinter.Tk()
canvas = tkinter.Canvas(master, width = 200, height = 100)
canvas.pack()
canvas.bind('<Enter>',show_text)
canvas.text = canvas.create_text(20, 30, text="hello")

mainloop()

解决方案

According to the canvas docs:

You can display one or more lines of text on a canvas C by creating a text object:

id = C.create_text(x, y, option, ...)

This returns the object ID of the text object on canvas C.

Now, you gotta modify the code something like this:

import tkinter
from tkinter import *


def show_text(event):
    print (canvas.itemcget(obj_id, 'text'))

master = tkinter.Tk()
canvas = tkinter.Canvas(master, width = 200, height = 100)
canvas.pack()
canvas.bind('<Enter>',show_text)
obj_id = canvas.create_text(20, 30, text="hello")

mainloop()

Follow up (see the documentation for Label.config:

import tkinter
from tkinter import *
from tkinter import ttk

def show_text(event):
    print (canvas.itemcget(canvas.text, 'text'))
    #The command of writing text 'hello' in sch_Label to replace the text 'the info shows here'
    sch_Label.config(text = 'hello!')

master = tkinter.Tk()
canvas = tkinter.Canvas(master, width = 200, height = 100)
canvas.pack()
canvas.bind('<Enter>',show_text)
canvas.text = canvas.create_text(20, 30, text="hello")

pad1 = ttk.Notebook(master)
pad1.pack(side=RIGHT, expand=1, fill="both")
tab1 = Frame(pad1)
pad1.add(tab1, text = "Schedule")
pad1.pack(side=RIGHT)
sch_Label = ttk.Label(tab1, text='The info shows here')
sch_Label.pack(side="top", anchor="w")
mainloop()

这篇关于Python 3.5:打印画布文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆