In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Python how to use canvas to achieve mobile and bind the keyboard, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
I. Task
Use multiple buttons or button + text box to realize the movement of different objects (ellipse, rectangle, fan) in all directions (top, bottom, left and right)
Effect picture:
2. Implement (1) lead the library and create the canvas import tkinter as tkwindow = tk.Tk () window.title ('my window') # # window size window.geometry ('300x350') # New canvas canvas=tk.Canvas (window,bg='blue',height=150,width=300) (2) draw the figure # draw the line x0memientin the line x0memirex1mem50 and 80line lineage canvas.createpainting line (x0memy0memx1memy1) # draw the ⚪ oval=canvas.create_oval (x0memy0line x1memy1) Fill='red') # draw a fan arc = canvas.create_arc (x0x 30, y 0y 30, x 1m 30, y 1m 30, start=0, extent=90) # draw a rectangle rect = canvas.create_rectangle (100,30,100x 20,30x 20) canvas.pack () (3) create button and text box # create text box entry = tk.Entry (window, show=None) entry.pack () # create text label = tk.Label (window, text=' circle: oval Line: line; sector: arc Rectangle: rect') label.pack () # Packaging # create a Buttonb=tk.Button (under window,text='', command=moveit) b.place (Xerox 120 authorship 280) up=tk.Button (on window,text='', command=moveup) up.place (Xerox 120 authorship yawn 220) left=tk.Button (window,text=' left', command=moveleft) left.place (xcircle 80 memyyboy 250) right=tk.Button (window,text=' right', command=moveright) right.place (Xerox 160) Function # move down def moveit (): obj = entry.get () # get the parameters of the input box Move the graphic if obj = = 'rect': canvas.move (rect,0,2) # the first parameter is the graphic elif obj = =' arc': canvas.move (arc, 0Magne2) # the first parameter is the graphic elif obj = = 'oval': canvas.move (oval, 0Magne2) # the first parameter is the graphic elif obj = =' line': canvas.move (line, 0) 2) # move def moveup () up: obj = entry.get () # get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,0,-2) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,0,-2) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,0,-2) # the first parameter is the graph elif obj = =' line': canvas.move (line,0) -2) # move def moveleft () to the left: obj = entry.get () # get the parameters of the input box Move the graph if obj = = 'rect': canvas.move (rect,-2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,-2,0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,-2,0) # the first parameter is the graph elif obj = =' line': canvas.move (line -2,0) # move def moveright () to the right: obj = entry.get () # to get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc, 2Magin0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval, 2P0) # the first parameter is the graph elif obj = =' line': canvas.move (line, 2) 0) 3. Complete code import tkinter as tkwindow = tk.Tk () window.title ('my window') # # window size window.geometry ('300x350') # New canvas canvas=tk.Canvas (window,bg='blue',height=150,width=300) # drawing line x0primex 50pr 50pr 80line lineage canvas.createtrainline (x0Quiny0menx1miny1) # draw ⚪ oval=canvas.create_oval (x0Query y0paramel x1flyer red') # draw a fan-shaped arc = canvas.create_arc (x0prix 30, y0m30, x1lane 30, y130x, start=0 Extent=90) # draw a rectangle rect = canvas.create_rectangle (100,30,100,20,30,20) canvas.pack () # create a text box entry = tk.Entry (window, show=None) entry.pack () # create text label = tk.Label (window, text=' circle: oval Line: line; sector: arc Rectangle: rect') label.pack () # Packaging # move down def moveit (): obj = entry.get () # get the parameters of the input box Move the graphic if obj = = 'rect': canvas.move (rect,0,2) # the first parameter is the graphic elif obj = =' arc': canvas.move (arc, 0Magne2) # the first parameter is the graphic elif obj = = 'oval': canvas.move (oval, 0Magne2) # the first parameter is the graphic elif obj = =' line': canvas.move (line, 0) 2) # move def moveup () up: obj = entry.get () # get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,0,-2) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,0,-2) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,0,-2) # the first parameter is the graph elif obj = =' line': canvas.move (line,0) -2) # move def moveleft () to the left: obj = entry.get () # get the parameters of the input box Move the graph if obj = = 'rect': canvas.move (rect,-2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,-2,0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,-2,0) # the first parameter is the graph elif obj = =' line': canvas.move (line -2,0) # move def moveright () to the right: obj = entry.get () # to get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc, 2Magin0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval, 2P0) # the first parameter is the graph elif obj = =' line': canvas.move (line, 2) 0) # create a Buttonb=tk.Button (under window,text='', command=moveit) b.place (Xerox 120 last Yili 280) up=tk.Button (on window,text='', command=moveup) up.place (Xerox 120 Eng 220) left=tk.Button (window,text=' left', command=moveleft) left.place (Xerox 80 meme Yee 250) right=tk.Button (window,text=' right', command=moveright) right.place (Xerox 160Coy 250) # # shows window.mainloop () IV, upgrade-bind keyboard event
Add keyboard binding events:
# implement keyboard binding window.bind ("", moveit) # pass a callback function window.bind (", moveleft) window.bind (", moveright) window.bind ("", moveup) in the second parameter
Complete code:
Import tkinter as tkwindow = tk.Tk () window.title ('my window') # # window size window.geometry ('300x350') # New canvas canvas=tk.Canvas (window,bg='blue',height=150,width=300) # drawing line x0meme x0memy1fly50pr 80pr 80lineweight canvas.createlightline (x0miny0memex1miny1) # draw ⚪ oval=canvas.create_oval (x0memy0memy1flyer redred`) # draw a fan-shaped arc = canvas.create_arc (x0prix 30, y0prix 30, x1lane 30, y1lane 30, start=0 Extent=90) # draw a rectangle rect = canvas.create_rectangle (100,30,100,20,30,20) canvas.pack () # create a text box entry = tk.Entry (window, show=None) entry.pack () # create text label = tk.Label (window, text=' circle: oval Line: line; sector: arc Rectangle: rect') label.pack () # Packaging # move down def moveit (event): obj = entry.get () # get the parameters of the input box Move the graphic if obj = = 'rect': canvas.move (rect,0,2) # the first parameter is the graphic elif obj = =' arc': canvas.move (arc, 0Magne2) # the first parameter is the graphic elif obj = = 'oval': canvas.move (oval, 0Magne2) # the first parameter is the graphic elif obj = =' line': canvas.move (line, 0) 2) # move def moveup (event) up: obj = entry.get () # get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,0,-2) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,0,-2) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,0,-2) # the first parameter is the graph elif obj = =' line': canvas.move (line,0) -2) # move def moveleft (event) to the left: obj = entry.get () # get the parameters of the input box Move the graph if obj = = 'rect': canvas.move (rect,-2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc,-2,0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval,-2,0) # the first parameter is the graph elif obj = =' line': canvas.move (line -2,0) # move def moveright (event) to the right: obj = entry.get () # get the parameters of the input box Move that graph if obj = = 'rect': canvas.move (rect,2,0) # the first parameter is the graph elif obj = =' arc': canvas.move (arc, 2Magin0) # the first parameter is the graph elif obj = = 'oval': canvas.move (oval, 2P0) # the first parameter is the graph elif obj = =' line': canvas.move (line, 2) 0) # create a Buttonb=tk.Button (under window,text='', command=moveit) b.place (Xerox 120, moveit 280) up=tk.Button (window,text='', command=moveup) up.place (window,text=' left', command=moveleft) left.place (window,text=' 80, command=moveright) right.place (window,text=' right) # realize keyboard binding window.bind ("", moveit) # pass a callback function window.bind ("", moveit) # the second parameter Moveleft) window.bind (", moveright) window.bind (", moveup) # # shows whether it is helpful for window.mainloop () to finish reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.