Because do the network class has to use, will not be used up or quite messy, but do look back to see TK is a lightweight and convenient one thing, first paste the code I used
classGraph:def __init__(self, Master): Self.frame=Frame (Master) self.label0= Label (Self.frame, text="Host"). Grid (Row=0, column=0) Self.hostport=stringvar () self.hostport.set ("%d"%tokenring.port) self.label01= Label (Self.frame, Textvariable=self.hostport). Grid (Row=0, Column=1) Self.label1= Label (Self.frame, text="Host Number"). Grid (Row=1, column=0) Self.entry1= Entry (self.frame,textvariable=self.hostport). Grid (Row=1, Column=1) Self.button2= Button (Self.frame, text="Restart", Command=self.restart). Grid (row=2, column=0) Self.button21= Button (Self.frame, text="Change Flag", command=self.givering). Grid (row=2, Column=1) Self.label3= Label (Self.frame, text=" "). Grid (Row=3, column=0) Self.label4= Label (Self.frame, text="tokenring Flag"). Grid (Row=4, column=0) Self.tokenringflag=Stringvar ()ifTokenring.flag = = 1: Self.tokenringflag.set ("Yes") Else: Self.tokenringflag.set ("NO") Self.label4= Label (Self.frame, Textvariable=self.tokenringflag). Grid (Row=4, Column=1) Self.label5= Label (Self.frame, text=" "). Grid (Row=5, column=0) Self.label6= Label (Self.frame, text="Terminal"). Grid (Row=6, column=0) Self.terminalist= Text (Self.frame, width=, height= 300) Self.terminalist.grid (Row=7, Column=0, columnspan=2) Self.frame.grid (Row=0, column=0)
Define the time with the name of the more wonderful, because I think the location of the number is more convenient for me to debug ... It is not recommended to name like me ...
Use part
if __name__ ' __main__ ' : time.sleep (1) = Tk () 'Host' # Set Position Graph = graph (root) Root.mainloop ()
The main use of this window is grid packaging, better than pack to use some
The grid can specify the approximate location for each element, although the exact location is not very precise, but generally enough for entry-level use
Define it first, and give me a chestnut.
Self.label0 = Label (self.frame, text="Host"). Grid (Row=0, column=0)
A label is defined, then a property is specified for the label, the first element is the parent control of the control, and here I use a large frame that contains all the controls I want to use, so that I will be more convenient if I want to change the whole window later. If you want to combine the window directly with this frame can also (but not to use
Grid packaging commonly used properties are row and column, specify the row number column number control generation location, the grid will automatically specify the size for you and arrange by row and column, if a line number is not used, it will be ignored, such as the first row to put a control, the third row to put a control, the second row is not specified, The actual resulting effect the second row is ignored and skipped, the third row of controls is placed on the second line of the position, of course, you can also
Self.terminalist = Text (Self.frame, width=, height= 300)
To customize the dimensions and use the
Self.terminalist.grid (row=7, column=0, columnspan=2)
ColumnSpan to let your control cross the line, for example this terminalist takes up a row of two columns of space
Content, you can use the
Self.entry1 = Entry (self.frame,textvariable=self.hostport). Grid (Row=1, Column=1)
Make the contents of the control variable, of course textvariable need you to define, it is worth mentioning that Self.hostport = Stringvar () defined variables should be set with get and set or value
Control content and variables are connected, changing the contents of the variable control changes immediately
These should be enough to initially experience TK and make a nice window out, as for all the controls available and what the controls have, it is recommended to refer to the documentation for use, more authoritative.
Oh yes, when the window is displayed,
Root.geometry (tokenring.position)
The properties of a window are specified by a string (very strange
' 400x400+0+0 ' # TK window Position
String is long x High + horizontal offset + vertical offset
X is the letter x, not multiplication sign
Finally attached to my ugly window ...
Python Tkinter Getting Started with