2013-10-30 120 views
1

我想創建一個畫布小部件,它允許用戶在畫布上繪製。我正在使用canvas.bind。我想要的是畫布的大小原本應該是32x32,但是當它顯示時,它應該像原始小部件的縮放版本,而不調整原始畫布的大小。我必須使用較小的尺寸(32x32)在繪製的圖片上進一步處理。如果畫布尺寸是32x32,則意味着它應該顯示爲512x512尺寸用於繪圖目的,但是32x32尺寸用於處理目的。我嘗試過使用canvas.Scale,但它似乎不會對此有所幫助,因爲它會調整原始小部件的大小。有人能指導我嗎?我使用python2.7。邏輯放大/縮小畫布

謝謝!

+0

親愛的@ user2566909如果你認爲張貼答案解決您的問題,或者至少這是一個有用的線索找到您正在尋找的解決方案,請不要猶豫,接受它。如果您不知道如何接受答案,在這裏您可以找到更多關於它的信息:http://meta.stackexchange.com/a/5235 –

回答

2

如果我完全理解你的問題,那麼我認爲這將是您的解決方案:

enter image description here

from __future__ import division 
import Tkinter 

ORIGINAL_WIDTH = 32 
ORIGINAL_HEIGHT = 32 
WINDOW_WIDTH = 512 
WINDOW_HEIGHT = 512 
WIDTH_RATIO = WINDOW_WIDTH/ORIGINAL_WIDTH 
HEIGHT_RATIO = WINDOW_HEIGHT/ORIGINAL_HEIGHT 

class App(Tkinter.Tk): 

    def __init__(self, *args, **kwargs): 
     Tkinter.Tk.__init__(self, *args, **kwargs) 
     self.canvas = canvas = Tkinter.Canvas(self, 
               highlightthickness = 0, 
               width = WINDOW_WIDTH, 
               height = WINDOW_HEIGHT) 
     self.canvas.pack() 
     # Create a list for holding canvas objects 
     self.scene = [] 
     # Draw objects and scale them 
     self.draw() 
     self.zoom() 


    def draw(self): 
     canvas = self.canvas 

     # Draw rectangles within the range of 0..32 x 0..32 
     rect1 = canvas.create_rectangle( 2, 0, 30, 2, fill='black') 
     rect2 = canvas.create_rectangle( 0, 2, 2, 30, fill='black') 
     rect3 = canvas.create_rectangle(30, 2, 32, 30, fill='black') 
     rect4 = canvas.create_rectangle( 2, 30, 30, 32, fill='black') 
     rect5 = canvas.create_rectangle(10, 10, 22, 22, fill='black') 

     self.scene.extend((rect1, rect2, rect3, rect4, rect5)) 

    def zoom(self): 
     canvas = self.canvas 
     # Scale all objects on scene 
     for item in self.scene: 
      canvas.scale(item, 0, 0, WIDTH_RATIO, HEIGHT_RATIO) 

App().mainloop()