1 | n/a | from tkinter import * |
---|
2 | n/a | |
---|
3 | n/a | # This example program creates a scroling canvas, and demonstrates |
---|
4 | n/a | # how to tie scrollbars and canvses together. The mechanism |
---|
5 | n/a | # is analogus for listboxes and other widgets with |
---|
6 | n/a | # "xscroll" and "yscroll" configuration options. |
---|
7 | n/a | |
---|
8 | n/a | class Test(Frame): |
---|
9 | n/a | def printit(self): |
---|
10 | n/a | print("hi") |
---|
11 | n/a | |
---|
12 | n/a | def createWidgets(self): |
---|
13 | n/a | self.question = Label(self, text="Can Find The BLUE Square??????") |
---|
14 | n/a | self.question.pack() |
---|
15 | n/a | |
---|
16 | n/a | self.QUIT = Button(self, text='QUIT', background='red', |
---|
17 | n/a | height=3, command=self.quit) |
---|
18 | n/a | self.QUIT.pack(side=BOTTOM, fill=BOTH) |
---|
19 | n/a | spacer = Frame(self, height="0.25i") |
---|
20 | n/a | spacer.pack(side=BOTTOM) |
---|
21 | n/a | |
---|
22 | n/a | # notice that the scroll region (20" x 20") is larger than |
---|
23 | n/a | # displayed size of the widget (5" x 5") |
---|
24 | n/a | self.draw = Canvas(self, width="5i", height="5i", |
---|
25 | n/a | background="white", |
---|
26 | n/a | scrollregion=(0, 0, "20i", "20i")) |
---|
27 | n/a | |
---|
28 | n/a | self.draw.scrollX = Scrollbar(self, orient=HORIZONTAL) |
---|
29 | n/a | self.draw.scrollY = Scrollbar(self, orient=VERTICAL) |
---|
30 | n/a | |
---|
31 | n/a | # now tie the three together. This is standard boilerplate text |
---|
32 | n/a | self.draw['xscrollcommand'] = self.draw.scrollX.set |
---|
33 | n/a | self.draw['yscrollcommand'] = self.draw.scrollY.set |
---|
34 | n/a | self.draw.scrollX['command'] = self.draw.xview |
---|
35 | n/a | self.draw.scrollY['command'] = self.draw.yview |
---|
36 | n/a | |
---|
37 | n/a | # draw something. Note that the first square |
---|
38 | n/a | # is visible, but you need to scroll to see the second one. |
---|
39 | n/a | self.draw.create_rectangle(0, 0, "3.5i", "3.5i", fill="black") |
---|
40 | n/a | self.draw.create_rectangle("10i", "10i", "13.5i", "13.5i", fill="blue") |
---|
41 | n/a | |
---|
42 | n/a | # pack 'em up |
---|
43 | n/a | self.draw.scrollX.pack(side=BOTTOM, fill=X) |
---|
44 | n/a | self.draw.scrollY.pack(side=RIGHT, fill=Y) |
---|
45 | n/a | self.draw.pack(side=LEFT) |
---|
46 | n/a | |
---|
47 | n/a | |
---|
48 | n/a | def scrollCanvasX(self, *args): |
---|
49 | n/a | print("scrolling", args) |
---|
50 | n/a | print(self.draw.scrollX.get()) |
---|
51 | n/a | |
---|
52 | n/a | |
---|
53 | n/a | def __init__(self, master=None): |
---|
54 | n/a | Frame.__init__(self, master) |
---|
55 | n/a | Pack.config(self) |
---|
56 | n/a | self.createWidgets() |
---|
57 | n/a | |
---|
58 | n/a | test = Test() |
---|
59 | n/a | |
---|
60 | n/a | test.mainloop() |
---|