Tkinter相當新穎。 我看了很多關於這個問題的帖子,但沒有爲我修復它。 我正在嘗試創建一個簡單的GUI,其中我有一個顯示圖像的畫布。 畫布是可滾動的,但滾動條不會伸展到畫布的大小。在Tkinter中將畫布尺寸拉伸到滾動條
以下是我的代碼的相關部分。這裏沒有實際顯示圖像的代碼,圖像由openFileDialog給出,但滾動條與圖像保持一致。
from Tkinter import *
import Image
import ImageTk
import numpy as np
import tkFileDialog
import os as os
class DIP(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.isOpenedYet = False
def initUI(self):
self.parent.title("Viewer")
self.pack(fill = BOTH, expand = 1)
menubar = Menu(self.parent)
self.parent.config(menu = menubar)
vsframe = Frame(self, width=500, height=500)
vsframe.grid(row = 1, column = 2, columnspan = 2, sticky = "nw")
hsframe = Frame(self, width=500, height=500)
hsframe.grid(row = 2, column = 1, rowspan = 2, sticky = "nw")
self.canv = Canvas(self, relief=SUNKEN)
self.canv.config(width=500, height=500)
self.canv.config(highlightthickness=0)
self.sbarV = Scrollbar(vsframe, orient=VERTICAL)
self.sbarH = Scrollbar(hsframe, orient=HORIZONTAL)
self.sbarV.config(command=self.canv.yview)
self.sbarH.config(command=self.canv.xview)
self.canv.config(yscrollcommand=self.sbarV.set)
self.canv.config(xscrollcommand=self.sbarH.set)
self.sbarV.pack(expand = YES, fill=BOTH)
self.sbarH.pack(expand = YES, fill=BOTH)
self.label2 = Label(self, border = 5)
self.label2.grid(row = 0, column = 1)
self.canv.grid(row = 1, column = 1, sticky = "nw")
#Open Image Menu
fileMenu = Menu(menubar)
fileMenu.add_command(label = "Open", command = self.onOpen)
menubar.add_cascade(label = "File", menu = fileMenu)
#menu for algorithms
basicMenu = Menu(menubar)
basicMenu.add_command(label = "Super Resolution-stub", command = self.SuperRes)
menubar.add_cascade(label = "Algorithms", menu = basicMenu)
我在想什麼?
謝謝,它已經與petem的幫助,但你的答案正是我所做的,謝謝。絕對要使用bg顏色提示.. – Ysch