python Graphics Programming from tkinter import *root=tk () root.title ("My test") Root.mainloop () #主循环以上是建立一个空白的窗体 ====================================== button1=button (root,text= "button1") button2= Button (root,text= "Button2") Button3=button (root,text= "Button3") text = entry (Root) listbox = Listbox (Root) button1.pack () Button2.pack () Button3.pack () Text.pack () Listbox.pack () Root.mainloop () The above is to place some small controls in the form =======================================def button1 (): Listbox.insert (end, "button1 pressed") Def button3 (): text_contents = text.get () listbox.insert (end, text_contents) Text.delete (0,end) Button1 = button (root, text= "button1", command = button1) Button3 = button (root, text= "Button3", command = button3) These are the actions that occur when you press Button1 and Button3, which is the interaction between the controls. =============================================== scrollbar = scrollbar (root, orient=vertical) listbox = Listbox (Root,yscrollcommand=scrollbar.set) scrollbar.configure (Command=listbox.yview) Scrollbar.pack () When the listbox is full, you won't see it. With the scroll bar, you can see that the .================================================= Placement widget divides the window into two parts, Part of the text box and button, and the other part controls the list box. Using the frame widget to do this, the frame widget looks like a container that can put other widgets inside it-just like the root window. Now create a pair of frames textframe = frame (root) listframe = frame (root) We've set the owner of each widget to the root window, and then we'll change its owner and apply it to our new design: Button1 = button (textframe, text= "Button1", command = button1) Button2=button (textframe,text= "Button2") Button3 = button (TextFrame , text= "Button3", command = button3) Text = entry (TextFrame) scrollbar = scrollbar (listframe, orient=vertical) Listbox = listbox (listframe,yscrollcommand= Scrollbar.set) scrollbar.configure (Command=listbox.yview) In fact, it doesn't work too much., unless we tell the TK widget where to place the widget in the frame, for this we can use the package method and three keywords: side, fill, and expand. In a word processor you can think of a side similar to a crosshair, which tells the widget frame which side they generally need to set, whether it's the top, bottom, left, or right-the default form is the way the group revolves around the center. In this case, in addition to the scroll bar used as the left aligner, we need to enclose everything else with a scrollbar around the right side of the list box. second option, fill, if it grows, tells TK which direction overflowed the small widget, in the x-axis direction (horizontal direction), y-axis direction (vertical direction), or two directions. In general, we can handle these buttons very well, but it wants the text box and the list box to use as much space as possible-so setting the text box to overflow in the x-axis and the list box overflowing in two directions-the scrollbar should resize the list box, so set it to overflow in the y-axis direction. Finally, the expand option tells the widget if it is possible to expand to free space-adding the expand=1 statement to the text box and list box package options to achieve this effect. Now, your code should look like this: text.pack (side=left, fill=x, expand=1) button1.pack (side=left) button2.pack (Side=left) button3.pack (side=left) listbox.pack (side=left,fill=both, expand=1) Scrollbar.pack (side=right, fill=y) Currently, all left buttons are packaged into two frames and placed in the root window just like they are packaged into other widgets. Remember, this way the framework should be expanded: Textframe.pack (fill=x) listframe.pack (fill=both,expand=1) ======================================= =================== keyboard events and mouse events We have some work, more or less--but its interface has been a bit impractical. If we can use the mouse and keyboard to make things more straightforward, these jobs will seem easier to handle. When you run the TK main loop, tap a key on the keyboard or move the mouse around to generate the event, you can bind the callback signal function to the button in the same way, so that the button is pressedWhen you go there will be a corresponding event. Now that we have a function to handle this, that's the callback signal for Button 3, but unfortunately, we can't use it now because the button's event callback signal is not the same as the mouse and keyboard. We have to limit it to other functions: def returninsert (Event): button3 () Then we register the callback signals for these events and look for the use of binding functions: Text.bind ("<Return>", returninsert) Here we use <Return> event code instead of <Enter> event code, which is very important, Because a second trigger is triggered when the mouse enters the list box. Now we want the user to use right-click to move items from the list box, which is pretty good for the same processing. First we write a callback signal as input to receive the event: def deletecurrent (Event): listbox.delete (ANCHOR) Then we bind the event to this callback signal: listbox.bind ("<Double-Button-3>", deletecurrent) right mouse button in TK is referred to as pushbutton-3 (do not confuse with the name of our third Form button callback signal) , because the second mouse button involves the middle key of the mouse. Finally, just in case they want to modify it, we can allow the user to copy a simple post back to the text box. We do not have a function to implement this function, so we will have to write some new code in the callback signal: def copytotext (Event): text.delete (0,end) current_ Note = listbox.get (ANCHOR) text.insert (0, current_note) then binds the event to the callback signal as before: Listbox.bind ("<Double-Button-1>", copytotext) You can not only limit these events, if you want to know more about which events you can bind, please see the TK internal library introduction. now seems to be the time to tidy up our program. Button 1 is a bad name for a function--there was aA similar name puts us in a bind, so let's not repeat it. We also have the opportunity to change some button functions, for example, we can set the input button to close the text box, Button 1 does not have a special effect, so we can remove it. Modifying these does not really make a functional change, but the program now looks like this: #!/usr/bin/python from tkinter import * root &NBSP;=&NBSP;TK () root.geometry ("600x400") root.title ("Note taker") def enter (): text_contents = text.get () listbox.insert (end, text_contents) text.delete (0,END ) def remove (): listbox.delete (ANCHOR) def save (): pass def Returninsert (Event): enter () def deletecurrent (event): remove () def copytotext ( Event): text.delete (0, end) current_note = listbox.get (ANCHOR) text.insert (0, current_note) textframe = frame (root) listframe = frame (root) enter_ Button = button (textframe, text= "Enter", command = enter) remove_button = button (textframe, text= "Remove", commAnd = remove) save_button = button (textframe, text= "Save", command = save) text = entry (TextFrame) scrollbar = scrollbar (listframe, orient =vertical) listbox = listbox (listframe, yscrollcommand=scrollbar.set, selectmode= EXTENDED) scrollbar.configure (Command=listbox.yview) text.bind ("<Return>", returninsert) listbox.bind ("<Double-Button-3>", deletecurrent) listbox.bind ("<Double-Button-1>", copytotext) text.pack (side=left, fill=x, expand=1) enter_button.pack (Side=LEFT) remove_button.pack (Side=left) save_button.pack (side=left) listbox.pack (Side=LEFT,fill=BOTH, expand=1) scrollbar.pack (side=right, fill=y) textframe.pack (Fill=X) listframe.pack ( fill=both, expand=1) root.mainloop () =================================================== Saving data between sessions can be done by saving the contents of the list to a file while the program is runningDownload it to install the program. To achieve this we will use a series version of the Pickle module--python, or use the aggregation of outgoing data types into a file. First we need to enter the module: Import pickle then we get a variable, easy to paste, this variable contains a list of easy stickers. In order to save it to a file, we just need to open the file to write and use the dump function. You may notice that the callback signal for the third button is empty, so let's change it to save the current easy-to-use list to a file: Def save (): F = file ("notes.db", "WB") Notes = listbox.get (0, end) pickle.dump (notes, f) Now, when you press the "Save" button, we can get a copy of our easy stickers on the desktop. This will not give us much help unless we have some way to download these easy-to-paste methods, so let's fill the list box correctly before starting the main loop: Try:f = file ("notes.db", "RB") notes = pickle.load (f) for item in notes:listbox.insert (End,item) f.close () except: Pass we need to use a try statement to bind everything together in case the file open throws an exception, that is, if the file "notes.db" does not exist, should throw an exception, generate a warning. However, if an exception is thrown, we will not really notice it, we just don't download anything easy to paste into the list only. #!/usr/bin/pythonfrom tkinter import *import pickleroot = tk () root.geometry (" 600x400 ") root.title (" Note taker ") Def enter (): text_contents = Text.get () listbox.insert (end, text_contents) tExt.delete (0,end) def remove (): listbox.delete (ANCHOR) def save (): f=file ("D:\\python-std\\notes.db", "W") notes=listbox.get (0,END) pickle.dump (notes,f) Def returninsert (event): enter () def Deletecurrent (Event): remove () Def copytotext (event): Text.delete (0, end) current_note = listbox.get (ANCHOR) text.insert (0, current_note) textframe = frame (root) listframe = frame (root ) enter_button = button (textframe, text= "Enter", command = enter) remove_ Button = button (textframe, text= "Remove", command = remove) save_button = button (textframe, text= "Save", command = save) text = entry (TextFrame ) Scrollbar = scrollbar(listframe, orient=vertical) Listbox = listbox (Listframe, yscrollcommand=scrollbar.set, selectmode=extended) scrollbar.configure (Command=listbox.yview) text.bind ("<Return>", Returninsert) Listbox.bind ("<Double-Button-3>", deletecurrent) listbox.bind ("<Double-Button-1>" , copytotext) text.pack (side=left, fill=x, expand=1) enter_button.pack (Side=LEFT) remove_ Button.pack (Side=left) save_button.pack (side=left) listbox.pack (side=left,fill=both, expand=1) Scrollbar.pack (side=right, fill=y) try: f=file ("d:\\python-std\\ Notes.db "," RB ") notes=pickle.load (f) for item in Notes: listbox.insert (End,item) f.close ( ) Except: pass textframe.pack (fill=x) listframe.pack (fill=BOTH, expand=1) Root.mainloop ()
Python graphics programming