我正在尝试使用以下代码来确定我的一些想法。但是它没有运行,因为画布在代码结束之前似乎没有任何尺寸,到那时为止做任何事都为时已晚。如何识别画布的尺寸以便在'divw = int(screen_width / 100)'行中使用它?
#!/usr/bin/python3
from tkinter import *
window = Tk()
window.title('Timetable')
window.attributes('-zoomed', True)
screen_width = window.winfo_width()
screen_height = window.winfo_height()
canvas = Canvas(window, width = window.winfo_screenwidth(), height = window.winfo_screenheight(), bg='steelblue')
canvas.pack()
image = PhotoImage(file='blank.png')
screen_width = window.winfo_width()
screen_height = window.winfo_height()
divw = int(screen_width / 100)
print (divw)
for i in range(0, divw):
print (i)
canvas.create_image(i * 100+50, 50, anchor = NW, image=image)
mainloop()
答案 0 :(得分:0)
答案 1 :(得分:0)
尝试这样做:
try:
import Tkinter as tk
except:
import tkinter as tk
class myCanvas(tk.Frame):
def __init__(self, root):
#self.root = root
self.w = 600
self.h = 400
self.canvas = tk.Canvas(root, width=self.w, height=self.h)
self.canvas.pack( fill=tk.BOTH, expand=tk.YES)
root.bind('<Configure>', self.resize)
def resize(self, event):
self.w = event.width
self.h = event.height
print ('width = {}, height = {}'.format(self.w, self.h))
root = tk.Tk()
root.title('myCanvas')
myCanvas(root)
root.mainloop()
为我工作。