简体   繁体   中英

Tkinter get width of button made with `create_window()`

I'm using Tkinter combined with Python Turtle Graphics and I want to be able to get the width of a button made using create_window() .

I am able to set the value using the code below, but I want to get the value rather than set it. How do I do this please?

import turtle
import tkinter as tk

screen = turtle.Screen()
canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)

canvas.itemconfig(w1, width=100)

turtle.done()

This will do it.

import turtle
import tkinter as tk

screen = turtle.Screen()
canvas = screen.getcanvas()

button = tk.Button(canvas.master, text="Press me")
w1 = canvas.create_window(0, 0, window=button)

canvas.itemconfig(w1, width=100)

button.update()
print(button.winfo_width())
print(button.winfo_height())

turtle.done()

Make sure to update before calling.winfo_width(). :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM