如何在python中创建无滚动条滚动的可滚动画布?

如何在python中创建无滚动条滚动的可滚动画布?,python,canvas,tkinter,scroll,Python,Canvas,Tkinter,Scroll,我一直在制作一个rpg,里面有各种各样的场景,我可以去看。我正在尝试在所有场景中设置一个大小相同的窗口,以便在较大的场景中,当角色靠近屏幕边缘时,窗口将滚动到屏幕的另一部分 from tkinter import* top = Tk() top.title("Lemonkingdom Quest") #For Directional Purposes up = False down = False left = False right = False #when a var has a b

我一直在制作一个rpg,里面有各种各样的场景,我可以去看。我正在尝试在所有场景中设置一个大小相同的窗口,以便在较大的场景中,当角色靠近屏幕边缘时,窗口将滚动到屏幕的另一部分

from tkinter import*
top = Tk()
top.title("Lemonkingdom Quest")

#For Directional Purposes
up = False
down = False
left = False
right = False

#when a var has a b in front of it, it refers
#to if they hero is present in that scene or location
#when false they are not there, when true they are
bcave1=False
bcastle=False
bmarket=False
bforest1 = False
bforest2 = False

class Chart:
        chart = Canvas(top,width=120,height=120,background="black",highlightthickness=0)
        chart.grid(row=0,column=0)
        cycle_period = 10
        cycle_period2 = 10



#This Class allows for creation of Actors, with pictures,
#Each Actor Has its own Canvas ID, assigned in the order they are created
class Person:
    def __init__(self,person,x,y,picture,items):
        self.person = person
        self.x = x
        self.y = y
        self.picture = picture
        self.items = items
        Chart.chart.create_image(x,y,image=picture,anchor=NW)
class Actor:
    def __init__(self,person,x,y,picture):
        self.person = person
        self.x = x
        self.y = y
        self.picture = picture
        Chart.chart.create_image(x,y,image=picture,anchor=NW)

Person("Hero",60,60,herodownstand,None) # ID = 1 when first created
#the globalized hero refers to the ID, after each scene
#all the Actors are deleted to allow for new actors to be inputted
#at that time the hero variable is changed to be the new ID of the
#hero that is created 
hero = Person("Hero",60,60,herodownstand,None)

# the xx and yy variables refer to the coordinates that the hero is at
# they help by being changed before they act to see if there is a wall in the
# way of them
xx = 60
yy = 60



class Item:
    def __init__(self,item,x,y,picture,owner):
        self.item = item
        self.x = x
        self.y = y
        self.picture = picture
        self.owner = owner
        Chart.chart.create_image(x,y,image=picture,anchor=NW)
    def grab(i,h):
        i.owner = h
    def discard(i,h):
         i.owner = None

Key=Item("Key",60,60,keypic,None)
#These Are Lists of Walls And other Objects that can be inputted

def market():
#240,120 is a space, 150,30 is a space
        s = 150
        t = 60
        Chart.chart.delete(ALL)
        Actor("Hero",s,t,herodownstand)
        global hero
        (hero,) = Chart.chart.find_closest(s,t,halo=None,start=None)
        global yy
        global xx
        xx = s
        yy = t
        global marketground
        global bforest2
        global bforest1
        global bmarket
        global bcastle
        global marketwall
        global bcave1
        bcave1=False
        bforest2 = False
        bforest1 = False
        bmarket = True
        bcastle = False
        #for (x,y) in marketground:
        #        Actor("",x,y,ground)
      #  Chart.chart.configure(width=270,height=210,background_image=tk.PhotoImage(ground.gif))
        Chart.chart.configure(width=270,height=210,background=None)
        Actor("door",150,0,marketwalldoortop)
        Actor("door",150,30,marketwalldoorbottom)
        print("just in the Market")
        for (x,y) in marketwall: 
                Actor("wall",x,y,marketwallpic)
                print("in the IF MARKET")

def castle():
        s = 150
        t = 150
        Chart.chart.delete(ALL)
        Actor("Hero",s,t,heroupstand)
        global hero
        sss= Chart.chart.find_closest(s,t,halo=None,start=None)
        (hero,)=sss
        global yy
        global xx
        yy = s
        xx = t
        global bforest2
        global bforest1
        global bcastle
        global bmarket
        global marketwall
        global bcave1
        bcave1=False
        print("at the castle MANN")
        bforest1=False
        bforest2=False
        bcastle = True
        bmarket = False
        Chart.chart.configure(width=270,height=180,background=None)
        for (x,y) in castlewall:
                Actor("wall",x,y,marketwallpic)
def forest1():
        s = 0
        t = 30
        Chart.chart.delete(ALL)
        Actor("Hero",s,t,herorightstand)
        global hero
        (hero,) = Chart.chart.find_closest(s,t,halo=None,start=None)
        global yy
        global xx
        xx = s
        yy = t
        global ground
        global bforest2
        global forest2wall
        global bforest1
        global bmarket
        global bcastle
        global marketwall
        global forest1wall
        global bcave1
        bcave1=False
        bforest2 = False
        bforest1 = True
        bmarket = False
        bcastle = False
        Chart.chart.configure(width=270,height=330,background=("black"))
        Actor("door",-30,30,gatepic)
        print("in the FOREST")
        for (x,y) in forest1wall: 
                Actor("wall",x,y,treepic)
                print("in the FOREST")
#        for (x,y) in treetop: 
 #               Actor("wall",x,y,treepic)
  #              print("in the FOREST")
def forest2():
        s = 210
        t = 0
        Chart.chart.delete(ALL)
        Actor("Hero",s,t,herodownstand)
        global hero
        (hero,) = Chart.chart.find_closest(s,t,halo=None,start=None)
        global yy
        global xx
        xx = s
        yy = t
        global cave1doorR
        global cave
        global bforest2
        global bforest1
        global bmarket
        global bcastle
        global marketwall
        global forest1wall
        global forest2wall
        global bcave1
        bcave1=False
        bforest2 = True
        bforest1 = False
        bmarket = False
        bcastle = False
        Chart.chart.configure(width=270,height=330,background=None)
        Actor("door",210,-30,gatepic)
        print("in the FOREST")
        for (x,y) in forest2wall: 
                Actor("wall",x,y,treepic)
                print("in the FOREST")
        for (x,y) in cave:
                Actor("wall",x,y,cavepic)
                print("by the cave")
        for (x,y) in cave1doorR:
                Actor("wall",x,y,cavedoorR)
def cave1():
        s = 180
        t = 30
        Chart.chart.delete(ALL)
        Actor("Hero",s,t,heroleftstand)
        global hero
        (hero,) = Chart.chart.find_closest(s,t,halo=None,start=None)
        global yy
        global xx
        xx = s
        yy = t
        global key1
        global cave1wall
        global cave1doorL
        global cave
        global bforest2
        global bforest1
        global bmarket
        global bcastle
        global marketwall
        global forest1wall
        global forest2wall
        global bcave1
        global Key
        bcave1 = True
        bforest2 = False
        bforest1 = False
        bmarket = False
        bcastle = False
        Chart.chart.configure(width=240,height=360,background=None)
        for (x,y) in cave1wall: 
                Actor("wall",x,y,cavepic)
        for (x,y) in key1:
            if Key.owner == None:
                Item("Key",x,y,keypic,None)
            else:
                None
        for (x,y) in cave1doorL:
                Actor("wall",x,y,cavedoorL)


#These Are the Moving Methods
class Move:
    def checkwallup(x):
        global up
        global down
        global left
        global right
        global xx
        global yy
        global marketwall
        global castlewall
        global bforest2
        global bmarket
        global bcastle
        global bforest1
        global forest1wall
        global forest2wall
        global cave
        global cave1doorR
        global cave1doorL
        global bcave1
        global cave1wall
        global key1
        global Key
        global hero
   #     global treetop
        yy = yy - 30
        pos = (xx,yy)
        if bmarket == True:
            if pos in marketwall:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)
            elif pos == (150,30):
                if Key.owner == hero:
                    Move.moveup(x)
                else:
                    Chart.chart.itemconfigure(x,image=heroupstand)

            elif pos == (150,0):
               if Key.owner == hero:
                    castle()
               else:
                    Chart.chart.itemconfigure(x,image=heroupstand)
            else:
                Move.moveup(x)
        elif bcastle == True:
            if pos in castlewall:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)
            else:
                Move.moveup(x)
        elif bforest1 == True:
            if pos in forest1wall:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)
            else:
                Move.moveup(x)
        elif bforest2 == True:
            if pos in forest2wall:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)
            elif pos in cave:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)
            elif pos == (210,-30):
                forest1()
            elif pos in cave1doorR:
                 yy = yy + 30
                 Chart.chart.itemconfigure(x,image=heroupstand)
            else:
                Move.moveup(x)
        elif bcave1==True:
            if pos in cave1wall:
                yy = yy + 30
                Chart.chart.itemconfigure(x,image=heroupstand)            
            elif pos in key1:
                if Key.owner == None:  
                    yy = yy + 30
                    Chart.chart.itemconfigure(x,image=heroupstand)
                else:
                    Move.moveup(x)
            elif pos in cave1doorL:
                 yy = yy + 30
                 Chart.chart.itemconfigure(x,image=heroupstand)
            else:
                Move.moveup(x)
    def checkwalldown(x):
        global up
        global down
        global left
        global right
        global xx
        global yy
        global marketwall
        global castlewall
        global bmarket
        global bcastle
        global bforest2
        global forest2wall
        global bforest1
        global forest1wall
        global cave
        global cave1doorL
        global cave1doorR
        global bcave1
        global cave1wall
        global key1
        global Key
        global hero
#       global treetop
        yy = yy + 30
        pos = (xx,yy)
        if bmarket == True:
            if pos in marketwall:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            else:
                Move.movedown(x)
        elif bcastle == True:
            if pos in castlewall:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos == (150,180):
                    market()
            else:
                Move.movedown(x)
        elif bforest1 == True:
            if pos in forest1wall:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos == (210,330):
                forest2()
#            if pos in treetop:
#                yy = yy - 30
#                Chart.chart.itemconfigure(x,image=herodownstand)
            else:
                Move.movedown(x)
        elif bforest2 == True:
            if pos in forest2wall:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos in cave:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos in cave1doorR:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            else:
                Move.movedown(x)
        elif bcave1 == True:
            if pos in cave1wall:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos in cave1doorL:
                yy = yy - 30
                Chart.chart.itemconfigure(x,image=herodownstand)
            elif pos in key1:
                if Key.owner == None:  
                    yy = yy - 30
                    Chart.chart.itemconfigure(x,image=herodownstand)
                else:
                    Move.movedown(x)
            else:
                Move.movedown(x)

    def checkwallleft(x):
        global up
        global down
        global left
        global right
        global xx
        global yy
        global marketwall
        global castlewall
        global bmarket
        global bcastle
        global bforest1
        global bforest2
        global forest2wall
        global forest1wall
        global cave
        global cave1doorR
        global cave1doorL
        global cave1wall
        global bcave1
        global key1
        global Key
        global hero
  #      global treetop
        xx = xx - 30
        pos = (xx,yy)
        if bmarket == True:
            if pos in marketwall:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            else:
                Move.moveleft(x)
        elif bcastle == True:
            if pos in castlewall:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            else:
                Move.moveleft(x)
        elif bforest1 == True:
            if pos in forest1wall:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            elif pos == (-30,30):
                    market()
   #         if pos in treetop:
   #             xx = xx + 30
   #             Chart.chart.itemconfigure(x,image=heroleftstand)
            else:
                Move.moveleft(x)
        elif bforest2 == True:
            if pos in forest2wall:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            elif pos in cave:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            elif pos == (60,240):
                    cave1()

            else:
                Move.moveleft(x)
        elif bcave1 == True:
            if pos in cave1wall:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            elif pos in cave1doorL:
                xx = xx + 30
                Chart.chart.itemconfigure(x,image=heroleftstand)
            elif pos in key1:
                if Key.owner == None:  
                    xx = xx + 30
                    Chart.chart.itemconfigure(x,image=heroleftstand)
                else:
                    Move.moveleft(x)
            else:
                Move.moveleft(x)
    def checkwallright(x):
        global up
        global down
        global left
        global right
        global xx
        global yy
        global marketwall
        global castlewall
        global bmarket
        global bcastle
        global bforest1
        global bforest2
        global forest2wall
        global forest1wall
        global cave
        global cave1doorR
        global cave1doorL
        global cave1wall
        global bcave1
        global key1
        global Key
        global hero
    #    global treetop
        xx = xx + 30
        pos = (xx,yy)
        if bmarket == True:
            if pos in marketwall:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            elif pos == (270,120):
                    forest1()
            else:
                Move.moveright(x)
        elif bcastle == True:
            if pos in castlewall:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            else:
                Move.moveright(x)
        elif bforest1 == True:
            if pos in forest1wall:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
 #           if pos in treetop:
  #              xx = xx - 30
   #             Chart.chart.itemconfigure(x,image=herorightstand)
            else:
                Move.moveright(x)
        elif bforest2 == True:
            if pos in forest2wall:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            elif pos in cave:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            elif pos in cave1doorR:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            else:
                Move.moveright(x)
        elif bcave1 == True:
            if pos in cave1wall:
                xx = xx - 30
                Chart.chart.itemconfigure(x,image=herorightstand)
            elif pos in key1:
                if Key.owner == None:                
                    xx = xx - 30
                    Chart.chart.itemconfigure(x,image=herorightstand)
                else:
                    Move.moveright(x)
            elif pos == (210,30):
                forest2()
            else:
                Move.moveright(x)
    def moveup(x):
        global up
        global down
        global left
        global right 
        up = True
        down = False
        right = False
        left = False
        Chart.chart.itemconfigure(x,state=NORMAL)
        Chart.chart.itemconfigure(x,image=heroupstand)
        for i in range(1,11):
                Chart.chart.move(x,0,-1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroupwalk1)
        for i in range(1,11):
                Chart.chart.move(x,0,-1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroupwalk2)
        for i in range(1,11):
                Chart.chart.move(x,0,-1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroupstand)
    def movedown(x):
        global up
        global down
        global left
        global right
        up = False
        down = True
        left =  False
        right = False
        Chart.chart.itemconfigure(x,state=NORMAL)
        Chart.chart.itemconfigure(x,image=herodownstand)
        for i in range(1,11):
                Chart.chart.move(x,0,1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=herodownwalk1)
        for i in range(1,11):
                Chart.chart.move(x,0,1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=herodownwalk2)
        for i in range(1,11):
                Chart.chart.move(x,0,1)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=herodownstand)          
    def moveleft(x):
        global up
        global down
        global left
        global right
        up = False
        down = False
        right = False
        left = True
        Chart.chart.itemconfigure(x,state=NORMAL)
        Chart.chart.itemconfigure(x,image=heroleftstand)
        for i in range(1,11):
                Chart.chart.move(x,-1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroleftwalk1)
        for i in range(1,11):
                Chart.chart.move(x,-1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroleftwalk2)
        for i in range(1,11):
                Chart.chart.move(x,-1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=heroleftstand)
    def moveright(x):
        global up
        global down
        global left
        global right
        right = True
        up = False
        down = False
        left = False
        Chart.chart.itemconfigure(x,state=NORMAL)
        Chart.chart.itemconfigure(x,image=herorightstand)
        for i in range(1,11):
                Chart.chart.move(x,1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(x,image=herorightwalk1)
        for i in range(1,11):
                Chart.chart.move(x,1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(1,image=herorightwalk2)
        for i in range(1,11):
                Chart.chart.move(x,1,0)
                Chart.chart.update()
                Chart.chart.after(Chart.cycle_period)
        Chart.chart.itemconfigure(1,image=herorightstand)
    def pause(x):
        for i in range(1,30):
            Chart.chart.move(x,0,0)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period)
    def attack():
        global xx
        global yy
class NPCMove:
    def moveup(x):
        for i in range(1,30):
            Chart.chart.move(x,0,-1)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period)
            Chart.chart.itemconfigure(x,image=(npc)+(x))
    def movedown(x):
        for i in range(1,30):
            Chart.chart.move(x,0,1)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period)
    def moveleft(x):
        for i in range(1,30):
            Chart.chart.move(x,-1,0)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period)
    def moveright(x):
        for i in range(1,30):
            Chart.chart.move(x,1,0)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period)
    def pause(x):
        for i in range(1,30):
            Chart.chart.move(x,0,0)
            Chart.chart.update()
            Chart.chart.after(Chart.cycle_period) 




top.bind(sequence="s",func=lambda x:Move.checkwalldown(hero))
top.bind(sequence="w",func=lambda x:Move.checkwallup(hero))
top.bind(sequence="a",func=lambda x:Move.checkwallleft(hero))
top.bind(sequence="d",func=lambda x:Move.checkwallright(hero))
top.bind(sequence="j",func=lambda x:Item.grab(Key,hero))
top.bind(sequence="k",func=lambda x:Item.discard(Key,hero))

market()

您只需省略滚动条,并确保画布的
scrollregion
属性包含您绘制的所有内容。然后,您可以通过编程方式调用
xview
yview
方法进行滚动

在这篇文章的结尾是一个可运行的例子。运行它,在窗口中单击,然后使用光标键移动红方块。请注意,当它到达边缘时,将导致画布滚动

这不是一个好的编码技术的例子。我到处都是硬编码的数字,但希望它能说明如何通过编程滚动画布。这方面的主要关键是:

  • 您需要定义一个大于可见窗口的
    滚动区域
  • 如果要使用此技术一次滚动一个像素,则需要将“单位”设置为1
  • 您可以调用
    xview
    yview
    方法来滚动画布
  • 这段代码应该适用于Python2.x;要让它在3.x上工作,您只需要更改import语句

    import Tkinter as tk
    import random
    
    class Example(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent, background="bisque")
            self.canvas = tk.Canvas(self, width=400, height=400)
    
            # define a larger virtual canvas 
            self.canvas.configure(scrollregion=(-400,-400,400,400), 
                                  borderwidth=0, highlightthickness=0,
                                  xscrollincrement=1, yscrollincrement=1)
    
            # arrange the widgets on the screen
            self.canvas.pack(fill="both", expand=True)
    
            # draw a object that we can move
            self.thing = self.canvas.create_rectangle(-20,-20,20,20, fill="red")
    
            # draw some random circles, so it's obvious when the canvas scrolls
            for i in range(100):
                x = random.randint(-300, 300)
                y = random.randint(-300, 300)
                radius = random.randint(10, 100)
                self.canvas.create_oval(x,y,x+radius, y+radius)
    
            # set bindings to move the thing around
            self.canvas.bind("<Left>",  lambda event: self.move(-5, 0))
            self.canvas.bind("<Right>", lambda event: self.move(5, 0))
            self.canvas.bind("<Up>",    lambda event: self.move(0,-5))
            self.canvas.bind("<Down>",  lambda event: self.move(0,5))
            self.canvas.bind("c",       lambda event: self.recenter())
    
            self.recenter()
            self.canvas.focus_set()
    
        def recenter(self):
            # center the viewport on the canvas
            # (because we know the window size, we know we want 
            # 50% of the window viewable)
            self.canvas.xview("moveto", "0.25")
            self.canvas.yview("moveto", "0.25")
    
        def make_visible(self):
            # determine the bounding box of the visible area of the screen
            (cx0,cy0) = (self.canvas.canvasx(0), self.canvas.canvasy(0))
            (cx1,cy1) = (self.canvas.canvasx(self.canvas.cget("width")), 
                         self.canvas.canvasy(self.canvas.cget("height")))
    
            # determine the bounding box of the thing to be made visible
            (x0,y0,x1,y1) = self.canvas.coords(self.thing)
    
            # determine how far off the screen the object is
            deltax = x0-cx0 if x0 <= cx0 else x1-cx1 if x1 >= cx1 else 0
            deltay = y0-cy0 if y0 <= cy0 else y1-cy1 if y1 >= cy1 else 0
    
            # scroll the canvas to make the item visible
            self.canvas.xview("scroll", int(deltax), "units")
            self.canvas.yview("scroll", int(deltay), "units")
    
        def move(self, deltax, deltay):
            # make sure we don't move beyond our scrollable region
            (x0,y0,x1,y1) = self.canvas.coords(self.thing)
            deltay = 0 if (deltay > 0 and y1 >= 400) else deltay
            deltay = 0 if (deltay < 0 and y0 <= -400) else deltay
            deltax = 0 if (deltax > 0 and x1 >= 400) else deltax
            deltax = 0 if (deltax < 0 and x0 <= -400) else deltax
    
            # move the item, then scroll it into view
            self.canvas.move(self.thing, deltax, deltay)
            self.make_visible()    
    
    if __name__ == "__main__":
        root = tk.Tk()
        Example(root).pack(fill="both", expand=True)
        root.mainloop()
    
    将Tkinter作为tk导入
    随机输入
    类示例(tk.Frame):
    定义初始化(自身,父级):
    tk.Frame.\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
    self.canvas=tk.canvas(self,宽度=400,高度=400)
    #定义更大的虚拟画布
    self.canvas.configure(scrollregion=-400,-400400),
    borderwidth=0,highlightthickness=0,
    xscrollincrement=1,yscrollincrement=1)
    #在屏幕上排列小部件
    self.canvas.pack(fill=“both”,expand=True)
    #画一个我们可以移动的物体
    self.thing=self.canvas.create_矩形(-20,-20,20,20,fill=“red”)
    #画一些随机的圆圈,这样画布滚动时就很明显了
    对于范围(100)内的i:
    x=random.randint(-300300)
    y=random.randint(-300300)
    半径=random.randint(10100)
    self.canvas.create_oval(x,y,x+半径,y+半径)
    #设置绑定以移动对象
    self.canvas.bind(“,lambda事件:self.move(-5,0))
    self.canvas.bind(“,lambda事件:self.move(5,0))
    self.canvas.bind(“,lambda事件:self.move(0,-5))
    self.canvas.bind(“,lambda事件:self.move(0,5))
    self.canvas.bind(“c”,lambda事件:self.recenter())
    self.recenter()
    self.canvas.focus_set()
    def重新中心(自身):
    #将视口居中放置在画布上
    #(因为我们知道窗口的大小,所以我们知道我们想要
    #窗口的50%(可查看)
    self.canvas.xview(“移动到”,“0.25”)
    self.canvas.yview(“移动到”,“0.25”)
    def使_可见(自身):
    #确定屏幕可见区域的边界框
    (cx0,cy0)=(self.canvas.canvasx(0),self.canvas.canvasy(0))
    (cx1,cy1)=(self.canvas.canvasx(self.canvas.cget(“宽度”)),
    self.canvas.canvasy(self.canvas.cget(“高度”))
    #确定要使其可见的对象的边界框
    (x0,y0,x1,y1)=self.canvas.coords(self.thing)
    #确定对象离屏幕的距离
    如果x0=cx1,则deltax=x0-cx0,否则为0
    如果y0=cy1,则deltay=y0-cy0,否则为0
    #滚动画布以使项目可见
    self.canvas.xview(“滚动”,整数(deltax),“单位”)
    self.canvas.yview(“滚动”,int(deltay),“单位”)
    def移动(自助、德尔泰、德尔泰):
    #确保我们不会超出可滚动区域
    (x0,y0,x1,y1)=self.canvas.coords(self.thing)
    如果(deltay>0且y1>=400)else deltay,则deltay=0
    如果(deltay<0且y0和x1>=400)else deltax,则deltay=0
    
    deltax=0如果(deltax<0和x0)所有这些代码真的有必要来说明您的问题吗?没有,但我不想意外地遗漏一些重要的内容。事后看来,我本可以删除大部分代码。@ethancoder:我用一个例子更新了我的答案。