Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand tkinter in Python GUI

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand tkinter in Python GUI". In daily operation, I believe many people have doubts about how to understand tkinter in Python GUI. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "how to understand tkinter in Python GUI"! Next, please follow the editor to study!

Show

Import tkinter if _ _ name__ = ='_ main__': win = tkinter.Tk () # set title win.title ("my world") # set width and height win.geometry ('800x600') # create canvas, set background color, high Wide canvas = tkinter.Canvas (win,bg='#EBEBEB',width=800,height=1200,xscrollincrement = 1, yscrollincrement = 1) x0Query y0 = 100100 # create a circle and fill it in red Outline white oval = canvas.create_oval (x0-5 str (x0) +','+ str (y 0) +')', fill='black',tags= "text") p1 = (x 0 line 0) x 0 oval = 120170 oval = canvas.create_oval (x 0-5 line) Fill='#ff0000', outline= "# 000000", tags= "node") canvas.create_text (x0, y0-10, text=' ('+ str (x0) +','+ str (y0) +'), fill='black',tags= "text") p2 = (x0, y0) # connection canvas.create_line (p1, p2, fill='#000000', tags= "line") canvas.pack (expand = tkinter.YES Fill = tkinter.BOTH) # Delete tags = "text" # canvas.delete ("text") win.mainloop ()

From tkinter import * import randomif _ _ name__ = ='_ main__': win = Tk () # set title win.title ("my world") # set width and height win.geometry ('800x600') frame = Frame (win) # create canvas, set background color, height Wide canvas = Canvas (frame,bg='#EBEBEB',width=800,height=800,scrollregion= (0recovero 2000 and 1200)) pos = [565.0575.0], [25.0185.0], [345.0750.0], [945.0685.0], [845.0655.0], [880.0660.0], [25.0230.0], [525.01000.0], [580.0volum 1175.0], [650.0Lim 1130.0] [1605.0620.0], [1220.0580.0], [1465.0200.0], [1530.0, 5.0], [845.0680.0], [725.0370.0], [145.0665.0], [415.0635.0], [510.0875.0], [560.0365.0], [300.0465.0] [520.0585.0], [480.0415.0], [835.0625.0], [975.0580.0], [1215.0245.0], [1320.0315.0], [1250.0400.0], [660.0180.0], [410.0250.0], [420.0555.0], [575.0665.0], [1150.0,1160.0], [700.0580.0] [685.0595.0], [685.0610.0], [770.0610.0], [795.0645.0], [720.0635.0], [760.0650.0], [475.0960.0], [95.0260.0], [875.0920.0], [700.0500.0], [555.0815.0], [830.0485.0] [1170.0], [830.0610.0], [605.0625.0], [595.0360.0], [1340.0725.0], [1740.0245.0]] for x in pos: canvas.create_oval (x-5, y-5, x + 5, y + 5, fill='#ff0000', outline= "# 000000", tags= "node") canvas.create_text Text=' ('+ str (x) +','+ str (y) +'), fill='black', tags= "text") length = len (pos) path = [x for x in range (length)] random.shuffle (path) for i in range (length-1): canvas.create_line (pos [I], pos [I], fill='#000000', tags= "line") hbar = Scrollbar (frame Orient=HORIZONTAL) # defines the horizontal scroll bar hbar.pack (side=BOTTOM, fill=X) # places the horizontal scroll bar on the bottom side, occupies the full X axis hbar.config (command=canvas.xview) # sets the horizontal scroll bar function and the canvas X axis scroll bar event binds vbar = Scrollbar (frame,orient=VERTICAL) # defines the vertical scroll bar vbar.pack (side=RIGHT, fill=Y) # places the vertical scroll bar on the far right Full Y-axis vbar.config (command=canvas.yview) # function to set vertical scroll bar and canvas Y-axis scroll bar event binding canvas.config (xscrollcommand=hbar.set, yscrollcommand=vbar.set) # set canvas XMagi Y-axis scroll bar function to bind canvas.pack (expand=True, fill=BOTH) frame.pack (expand=True, fill=BOTH) # delete tags = "text" # canvas.delete ("text") win.mainloop ()

Multithreading

Import timefrom tkinter import * import randomimport threadingpos = [[565.0575.0], [25.0185.0], [345.0750.0], [945.0685.0], [845.0655.0], [880.0660.0], [25.0230.0], [525.0ju 1000.0], [580.0jue 1175.0], [650.0jue 1130.0], [1605.0620.0], [1220.0580.0] [1465.0200.0], [1530.0, 5.0], [845.0680.0], [725.0370.0], [145.0665.0], [415.0635.0], [510.0875.0], [560.0365.0], [300.0465.0], [520.0585.0], [480.0415.0], [835.0625.0], [975.0580.0] [1215.0245.0], [1320.0315.0], [1250.0400.0], [660.0180.0], [410.0250.0], [420.0555.0], [575.0665.0], [1150.0,1160.0], [700.0580.0], [685.0595.0], [685.0610.0], [770.0610.0] [795.0645.0], [720.0635.0], [760.0650.0], [475.0960.0], [95.0260.0], [875.0920.0], [700.0500.0], [555.0815.0], [830.0485.0], [1170.0, 65.0], [830.0610.0], [605.0625.0], [595.0360.0] [1340.0725.0], [1740.0245.0] win = Tk () # set title win.title ("my world") # set width and height win.geometry ('800x600') frame = Frame (win) # create canvas Set background color, high Wide canvas = Canvas (frame,bg='#EBEBEB',width=800,height=800,scrollregion= (0recovero 2000,1200)) for x text y in pos: canvas.create_oval (x-5, y-5, x + 5, y + 5, fill='#ff0000', outline= "# 000000", tags= "node") canvas.create_text (x str y-10, text=' ('+ str (x) +','+ str (y) +'), fill='black', tags= "text") hbar = Scrollbar (frame Orient=HORIZONTAL) # defines the horizontal scroll bar hbar.pack (side=BOTTOM, fill=X) # places the horizontal scroll bar on the bottom side, occupies the full X axis hbar.config (command=canvas.xview) # sets the horizontal scroll bar function and the canvas X axis scroll bar event binds vbar = Scrollbar (frame,orient=VERTICAL) # defines the vertical scroll bar vbar.pack (side=RIGHT, fill=Y) # places the vertical scroll bar on the far right Full Y-axis vbar.config (command=canvas.yview) # function to set vertical scroll bar and canvas Y-axis scroll bar event binding canvas.config (xscrollcommand=hbar.set, yscrollcommand=vbar.set) # set canvas XMagi Y-axis scroll bar function to bind canvas.pack (expand=True, fill=BOTH) frame.pack (expand=True) to vertical scroll bar Fill=BOTH) def run (): length = len (pos) path = [x for x in range (length)] random.shuffle (path) time.sleep (1) for i in range (length-1): canvas.create_line (pos [I], pos [I + 1], fill='#000000', tags= "line", arrow=LAST Width=1) time.sleep (1) if _ _ name__ = ='_ _ main__': # Delete tags = "text" # canvas.delete ("text") t = threading.Thread (target=run) t.start () win.mainloop ()

Provisional version

Personal knowledge is still not enough, there are a lot of small bug, let me think again

Import timefrom tkinter import * import randomimport sysimport threadingclass TSP (object): def _ _ init__ (self,root Cities:list [list]): self.__root = root self.__root.geometry ('800x600') self.__root.title ("TSP Ant Colony algorithm (n: initialize e: start search s: stop search c: continue search Q: exit the program)") self.__frame = Frame (root) self.__canvas = Canvas (self.__frame,bg='#EBEBEB',width=800,height=800,scrollregion= (0mem0) Hbar = Scrollbar (self.__frame, orient=HORIZONTAL) # defines the horizontal scroll bar hbar.pack (side=BOTTOM, fill=X) # places the horizontal scroll bar on the bottom side, occupies the full X-axis hbar.config (command=self.__canvas.xview) # sets the horizontal scroll bar function and binds the canvas X-axis scroll bar event vbar = Scrollbar (self.__frame Orient=VERTICAL) # defines the vertical scroll bar vbar.pack (side=RIGHT, fill=Y) # places the vertical scroll bar on the far right, occupies the full Y axis vbar.config (command=self.__canvas.yview) # sets the function of the vertical scroll bar and binds the canvas Y axis scroll bar event self.__canvas.config (xscrollcommand=hbar.set, yscrollcommand=vbar.set) # sets the X of the canvas Y-axis scroll bar function binds to vertical scroll bar self.__canvas.pack (expand=True, fill=BOTH) self.__frame.pack (expand=True, fill=BOTH) self.setCity (cities) self.__lock = threading.RLock () # thread lock self.__bindEvents () self.__new () def setCity (self,cities:list [list]): self.__cities = cities def sefPathsAndValues (self Paths: [list], values:list): self.__paths = paths self.__values = values # key response program def _ _ bindEvents (self): self.__root.bind ("Q", self.__quite) # exit program self.__root.bind ("n", self.__new) # initialize self.__root.bind ("e") Event=None): self.__lock.acquire () # Lock self.__running = False # flag bit self.__lock.release () # release lock for x, y in self.__cities: self.__canvas.create_oval (x-5, y-5, x + 5, y + 5, fill='#ff0000', outline= "# 000000" Tags= "node") self.__canvas.create_text (x, y-10, text=' ('+ str (x) +','+ str (y) +')', fill='black', tags= "text") def _ _ quite (self Path:list): self.__canvas.delete ("line") I = 0 while self.__running: if I = = len (self.__cities)-1: break p1, p2 = self.__ self.__ [path [I]], self.__ companies [path [I + 1]] self.__canvas.create_line (p1, p2, fill='#000000' Event=None): self.__lock.acquire () # Lock self.__running = True # Mark bit self.__lock.release () # release Lock while self.__running: X = random.randint (10100) label = Label (self.__canvas,text= "Best path Total distance:" + str (x)) .place (Xerox 10) Event=None): self.__lock.acquire () # Lock self.__running = True # flag bit self.__lock.release () # release lock def mainloop (self): self.__root.mainloop () if _ _ name__ ='_ _ main__': tsp = TSP (Tk (), cities) tsp.mainloop () so far The study on "how to understand tkinter in Python GUI" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report