Python 3.5: Print Canvas Text

2024/11/16 23:49:50

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()
Answer

According to the canvas docs:

You can display one or more lines of text on a canvas C by creating atext 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()

getting-text-from-canvas

Follow up (see the documentation for Label.config:

import tkinter
from tkinter import *
from tkinter import ttkdef 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()

enter image description here

https://en.xdnf.cn/q/118874.html

Related Q&A

What is the most efficient way to match keys from a dictionary to data in text file

Say I have the following dictionary:data=[a 1 : A, b 2 : B, c 3 : C, d 4 : D]and a .txt file which reads:Key a 1 b 2 c 3 d 4 Word as box cow dig(note values are seperated by \t TAB char…

Converting an excel file to a specific Json in python using openpyxl library

I have the Excel data with the format shown in the image preview. How can I convert it into a JSON using Python? Expected Output: file_name = [ { A: Measurement( time=10, X1=1, X2=4 ), B: Measurement(…

Why this algorithm can sort data in descending order

I study python programming and try to sort data in descending order.#sort1 below is successfully sorted but I cannot understand why this happen. Also, data[i], data[data.index(mn)] = data[data.index(m…

Processing.py - Unknown Error on Class Definition

I have no idea how to fix this error. Maybe theres an open parenthesis or quotation mark somewhere before this line?What is wrong with this code?Class Ribbon: # I got an error on this line! def __ini…

How can I implement a stopwatch in my socket Python program

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to t…

Making a quiz with shuffled questions

I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.There are 28 aircraft that I have added to the quiz. For example - Where i…

How to create a timer that resets for quiz games?

I trying to create a timer for my quiz game. It should reset after every right question. But problem with my code is that it keeps increasing speed after every time it resets. timeCount = 30 def countd…

Stucking of python multiprocessing.pool

i have multiprocessing script with "pool.imap_unordered". I ran into a problem when the script got stuck but i check CPU usage — nothing happening (by "top" command on ubuntu). Goi…

python: merge two csv files

I have a problem while Im doing my assignment with python. Im new to python so I am a complete beginner.Question: How can I merge two files below?s555555,7 s333333,10 s666666,9 s111111,10 s999999,9and…

Generate a custom formated string with python

I have a Javascript code that generates a string (similar to uuid) stringHere it is the js code: var t = "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx", i = (new Date).getTime(); return e = t.repla…