宽度或\和高度=尺寸
我有frame 300x300
和一个canvas 600x600
(框架内的画布)。
我想要frame
的固定大小,因为如果更改canvas
的大小->我的框架大小变得等于画布的宽度和高度!如何设置frame
的固定大小,这样我的canvas
将不会显示?
from tkinter import *
class MainWindow():
def __init__(self):
self.canvas_width = 600 # more than frame width
self.canvas_height = 600 # more than frame height
self.frame = Frame(root,width=300,height=300) # should be fixed in the window
self.frame.grid(row=0,column=0)
self.canvas = Canvas(self.frame,bg='bisque',width=self.canvas_width,height=self.canvas_height,scrollregion=(0,self.canvas_width,self.canvas_height))
self.frame.propagate(False)
self.hbar = Scrollbar(self.frame,orient=HORIZONTAL)
self.hbar.pack(side=BOTTOM,fill=X)
self.hbar.config(command=self.canvas.xview)
self.vbar = Scrollbar(self.frame,orient=VERTICAL)
self.vbar.pack(side=RIGHT,fill=Y)
self.vbar.config(command=self.canvas.yview)
# self.canvas.config(width=300,height=300)
self.canvas.config(xscrollcommand=self.hbar.set,yscrollcommand=self.vbar.set)
self.canvas.pack(side=LEFT,expand=True,fill=BOTH)
self.canvas.bind("<ButtonPress-1>",self.scroll_start)
self.canvas.bind("<B1-Motion>",self.scroll_move)
self.grid(self.canvas)
def grid(self,canvas):
for line in range(0,self.canvas_height,10):
canvas.create_line([(line,0),(line,self.canvas_height)],fill='#d9d9d9')
for line in range(0,10):
canvas.create_line([(0,line),(self.canvas_width,line)],fill='#d9d9d9')
def scroll_start(self,event):
self.canvas.scan_mark(event.x,event.y)
def scroll_move(self,event):
self.canvas.scan_dragto(event.x,event.y,gain=1)
if __name__ == '__main__':
root = Tk()
root.geometry('850x700')
MainWindow()
root.mainloop()