Python GUI interface development
Broad Code Framework
Class Application (Frame):
def __init__ (self,master=none):
frame.__init__ (self,master)
self.pack ()
Self.createwidgets ()
def createwidgets (self):
Self.hellolabel = Label (self, text= ' Hello,world ')
Self.helloLabel.pack ()
Self.apktoolbutton = Button (self,text= ' decompile apk ', command=self.debuildapk)
Self.apktoolButton.pack ()
Self.quitbutton = Button (self, text= ' Quit ', command=self.quit)
Self.quitButton.pack ()
if __name__ = = ' __main__ ':
app = Application ()
#设置窗口标题
App.master.title ( ' Hello wolrd ')
#主消息循环
App.mainloop ()
Label text Dynamic modification
The label or button has no functions such as settext or set.
However, you can use the assignment method directly to modify the
Label=enter (root,text= "My name is Rocky")
label["text"]= "My name is Ben"
Verified, you can dynamically modify the text on the label. Utf-8 Code
#coding =utf-8//This is the use of UTF8 encoding method, you can join the Python head alone. #-*-coding:cp936-*-import sys reload (SYS) sys.setdefaultencoding (' Utf-8 ')
Python machine learning Algorithm library: Sklearn
Install Sklearn
1. Before installing Sklearn, we need to install the NUMPY,SCIPY function library first.
NumPy Download Address:
Http://sourceforge.net/projects/numpy/files/NumPy
scipy Download Address:
Http://sourceforge.net/projects/scipy/files/Scipy
Download the Python version of the corresponding native.
2. Install Sklearn Machine Learning Library
Download Address:
Https://github.com/scikit-learn/scikit-learn
After downloading the compressed package, unzip the compressed package. Use CMD to enter the Sklearn folder and execute
Python setup.py Install
Sklearn Use
Training SVM Classifier, classifying according to classifier model
Class classify:
def __init__ (self):
Self.model = SVM. SVC () "" ""
data:
X = [[0, 0], [1, 1], [1, 0]] # training samples
y = [0, 1, 1] # training Target
"" "
def Createmodel (self,samples,target):
self.model.fit (samples,target)
def predictsample (self, Sample): Return
self.model.predict (sample)
An efficient model persistence module Joblib is provided in Sklearn to save the model to the hard disk.
From sklearn.externals import joblib
#lr是一个LogisticRegression模型
joblib.dump (LR, ' Lr.model ')
lr = Joblib.load (' Lr.model ')
TK related modules
http://www.jianshu.com/p/e88ba6c5f65e
Python 3.4 TK Support library description
Tkinter.scrolledtext
Text widget with a vertical scroll bar built in.
Tkinter.colorchooser Dialog to let the
user choose a color.
Tkinter.commondialog
Base class for the "dialogs defined in the" modules listed here.
Tkinter.filedialog
Common dialogs to allow the user to specify a file to open or save.
Tkinter.font
Utilities to help work with fonts.
Tkinter.messagebox
Access to Standard Tk dialog boxes.
Tkinter.simpledialog
Basic dialogs and convenience functions.
TKINTER.DND
drag-and-drop support for Tkinter.
Turtle
Turtle Graphics in a Tk window.
Description of the python2.7 TK Support library description
Scrolledtext
Text widget with a vertical scroll bar built in.
Tkcolorchooser Dialog to let the
user choose a color.
Tkcommondialog
Base class for the "dialogs defined in the" modules listed here.
Tkfiledialog
Common dialogs to allow the user to specify a file to open or save.
Tkfont
Utilities to help work with fonts.
Tkmessagebox
Access to Standard Tk dialog boxes.
Tksimpledialog
Basic dialogs and convenience functions.
TKDND
drag-and-drop support for Tkinter.
Turtle
Turtle Graphics in a Tk window.
Text text box: supporting scroll bars
#-*-coding:utf-8-*-"" "text Box sample implementation features: CTRL + A full selection of text, vertical scroll bar, horizontal scroll bar (do not automatically wrap) automatic scaling who knows the method of selecting all text is
Would you like to return to ' break '? http://blog.csdn.net/xxb2008 "" "Import Tkinter class MainFrame (tkinter.frame): Def __init__ (Self, Master=none): tkinter.frame.__init__ (self, Master) Self.grid (row=0, column=0, sticky= "n Sew ") Self.createframe () def createframe (self): Label_frame_top = Tkinter.labelfram E (self) #label_frame_top. Pack () Label_frame_center = Tkinter.labelframe (self) l Abel_frame_center.pack (fill= "x") Lfc_field_1 = Tkinter.labelframe (label_frame_center) lfc_fie Ld_1.pack (fill= "x") self.lfc_field_1_l = Tkinter.label (lfc_field_1, text= "file path:", width=10) s Elf.lfc_field_1_l.pack (fill= "Y", Expand=0, side=tkinter.left) Self.lfc_field_1_b = Tkinter.button (Lfc_fielD_1, text= "clear:", width=10, Height=1, Command=self.cleartext) self.lfc_field_1_b.pack (fill= "None", Expand=0, Side=tkinter.right, anchor=tkinter.se) ######### #文本框与滚动条 self.lfc_field_1_t_sv = Tkinter.scro Llbar (Lfc_field_1, orient=tkinter.vertical) #文本框-vertical scroll bar self.lfc_field_1_t_sh = Tkinter.scrollbar (lfc_field_ 1, orient=tkinter.horizontal) #文本框-transverse scroll bar self.lfc_field_1_t = Tkinter.text (Lfc_field_1, height=15, Yscroll Command=self.lfc_field_1_t_sv.set, xscrollcommand=self.lfc_field_1_t_sh.se T, wrap= ' none ') #设置滚动条-no wrap #滚动事件 self.lfc_field_1_t_sv.config (command=self.lfc_field_1_t.yvie W) self.lfc_field_1_t_sh.config (command=self.lfc_field_1_t.xview) #布局 self.lfc_ Field_1_t_sv.pack (fill= "Y", Expand=0, Side=tkinter.right, ANCHOR=TKINTER.N) self.lfc_field_1_t_sh.pack (fill= "X", Expand=0, Side=tkinter. BOTTOM, ANCHOR=TKINTER.N) self.lfc_field_1_t.pack (fill= "x", Expand=1, Side=tkinter.left) #绑定事
Pieces of Self.lfc_field_1_t.bind ("", Self.selecttext) Self.lfc_field_1_t.bind ("", Self.selecttext) ######### #文本框与滚动条end label_frame_bottom = Tkinter.labelframe (self) #label_fra Me_bottom.pack () Pass #文本全选 def selecttext (Self, event): Self.lfc_field_
1_t.tag_add (Tkinter.sel, "1.0", Tkinter.end) #self. Lfc_field_1_t.mark_set (Tkinter.insert, "1.0") #self. Lfc_field_1_t.see (Tkinter.insert) return "break" #为什么要return ' break ' #文本清空 de F cleartext (self): self.lfc_field_1_t.delete (0.0, tkinter.end) def main (): root = Tkinter. Tk () root.columnconfigure (0, weight=1) root.rowconfigure (0, Weight=1) root.geometry (' 640x360 ') #设置了主窗口的初始大小960x540 800x450 640x360 main_frame = MainFrame (root) main_frame.mainloop () If __name__ = "__main
__ ": Main () Pass
Tkinter Components
Tkinter provides a variety of controls, such as buttons, labels, and text boxes, used in a GUI application. These controls are often referred to as controls or parts.
There are currently 15 kinds of tkinter components. We present these parts as well as a brief introduction in the following table:
The control describes
the button button control, and the button is displayed in the program.
Canvas The canvas control, display a graphical element such as a line or text
Checkbutton A multiple-marquee control; for providing multiple selection boxes in a program
Entry input controls; for displaying simple text content
Frame framework control; Displays a rectangular area on the screen, used as a container
label Label control; you can display text and bitmap
ListBox controls ; In the ListBox window widget is used to display a list of strings
to the User Menubutton menu button control, because the Display menu item.
menu Menu control; Displays the Menus bar, Drop-down menu, and pop-up
menu message control; Used to display multiline text, similar to label
Radiobutton radio Button control , display a radio button state
Scale range control, display a numeric scale, Scrollbar a scroll bar control for the range of numbers in the output limit, and use when the content exceeds the visual area, such as a list box ...
Text control; Used to display multiline text
toplevel container controls; To provide a separate dialog box, and frame to compare similar
spinbox input controls; similar to entry, However, you can specify the input range value
Panedwindow panedwindow is a window layout-managed plug-in that can contain one or more child controls.
Labelframe Labelframe is a simple container control. Commonly used with complex window layouts.
Tkmessagebox is used to display the message box for your application.
Standard Properties
Standard properties are common properties of all controls, such as size, font, color, and so on.
Property describes the size of the
Dimension control; color control colors; font control fonts;
anchor anchor points;
Relief control style;
Bitmap bitmap;
Cursor cursor;
Set Text Property
Not editable
CONFIG. Text.bind ("", Lambda e: "Break")
Delete Content
CONFIG. Text.delete (1.0,end)
Set color
CONFIG. Text[' FG '] = ' black '
Insert Content
CONFIG. Text.insert (About_message)
Text Save
#直接保存
with open (OS.GETCWD () +r ' Log.txt ', ' w+ ') as FB:
Fb.write (CONFIG. Text.get (0.0, ' end '))
#交互式保存
r = filedialog.asksaveasfilename (title= ' Save file ', INITIALDIR=OS.GETCWD (), initialfile= ' MyLog.txt ')