Python note – Tkinter

Create window container

Use n=tk.Tk() can create a main window. 

Use n.geometry(“width x height”) can set the width and height of the main window. 

Use n.title(“text”) can set the title of the main window. 

Use n.mainloop() to make the function into apply mode with user. 

import tkinter as tk 

win = tk.Tk() 

win.geometry(“450×100”) 

win.title(“This is main window”) 

win.mainloop() 

FUNCTION

width set the width of the unit.  

height set the height of the unit. 

text set the text of the unit. 

textvariable set the dynamic text of the variable for the unit. 

tk.StringVar() data type is string. 

tk.IntVar() data type is integer. 

tk.DoubleVar() data type is float. 

if already set the textvariable of the unit, than the text will not work , and need use variable.set(text) to set the text of the unit.  

variable.get() can get the value from the varriable. 

background&bg set the background color of the unit. 

foreground&fg set the unit color of the text. 

font set the text size and type of the unit. 

padx set the x distance between the unit and the container. 

pady set the y distance between the unit and the container. 

UNIT

Use button= tk.Button(n,…,…,…,…) to create a button unit and set the condition fot it. 

command set a function for the unit to start when it be touched. 

Use text= tk.Text(n,…,…,…,…) to create a text unit and set the condition fot it. 

state=tk.NORMAL & tk.DISABLED can set the text available to edit or not. 

text.insert(tk.INSERT,”what you want to enter.”) can additional word into unit 

text.insert(tk.END,”The last word you want enter.”) can additional word into unit than end the text unit. 

text.config(state=tk.DISABLED) can change the condition of the unit. 

text=tk.Entry(n,…,…,…) can creat a text entry unit for user to enter the data. 

 

Use checkbutton=tk.Checkbutton(n,text,variable,command,…) to create a multiple selection options. 

Set unit shape type 

Use unit.pack() can make unit be a square shape to show. 

side=” (left right top bottom)” assignation position for unit at container. 

Use unit.grid() can make unit be a grid shape to show. 

row=0 ,columm=1 can set unit row and columm position at container. 

padx=1, pady=1 can set the x and y distance between the unit and the container. 

rowspan=1 , colummspan=1 can admix rowspan or colummspan. 

sticky= e(right), w(left), n(top) ,s(bottom) can set array position of the unit. 

Use unit.place() can set unit at particular position. 

x=50,y=50 can set position for the unit. 

relx=0~1,rely=0~1 can set position for the unit. 

anchor=center ,ne(right top) ,nw(left top) ,se(right bottom) ,sw(left bottom) ,n(medium top) ,s(medium bottom) ,e(right medium) ,w(left medium) set the standard point for the unit. 

Create more container

Use frame=tk.Frame(n,…,…) to create more window container. 

width,height set the width and height of the frame.  

background&bg set the background color of the frame. 

發佈留言