tkinter-- test exit (1)

1. Basic window test

Create a file test.py

TK Tkinter AS Import
window tk.Tk = () creates a window #
window.title ( 'test window')
window.geometry ( '300x100') window size #

text = tk.Text(window)
text.insert(tk.INSERT,'hello')

text.pack()

tk.mainloop ()

 

Run pyinstaller -D -w test.py

Will generate exe files

 

2. Run the test

print ('Python script to exe test program')
count = 0
while count < 10:
print ("count = " + str(count) +"\n")
count = count + 1

 

3. writing computer programs and generate EXE

Caculater.py
  . 1  # filename: Caculater.py 
  2  
  . 3  Import Tkinter, Time, decimal, Math, String
   . 4  
  . 5 the root tkinter.Tk = () # create a window 
  . 6 root.title ( ' Calculator ' ) # window title 
  . 7 root.resizable (0 , 0) # window size (block size adjustable body, respectively, x, y-direction variability;) 
  . 8  global cuncu, vartext, Result, Fuhao # full global variable 
  . 9 Result = Fuhao = None
 10 vartext = tkinter.StringVar ()
 . 11 cuncu = [] # deposit 
12 is  
13 is class anjianzhi:
 14     global cuncu, vartext, result, fuhao
 15     def __init__(self,anjian):
 16         self.anjian = anjian  #按键
 17     def jia(self):  #
 18         cuncu.append(self.anjian)
 19         vartext.set( ''.join(cuncu))
 20     def tui(self):
 21         cuncu.pop()
 22         vartext.set(''.join(cuncu))
 23     def clear(self):
 24         cuncu.clear()
 25         vartext.set('')
 26         result = None
 27         fuhao = None
 28     def zhengfu(self):
 29         if cuncu[0]:
 30             if cuncu[0] == '-':
 31                 cuncu[0] = '+'
 32             elif cuncu[0] == '+':
 33                 cuncu[0] = '-'
 34             else:
 35                 cuncu.insert(0, '-')
 36         vartext.set(''.join(cuncu))
 37     def xiaoshudian(self):
 38         if cuncu.count('.') >= 1:
 39             pass
 40         else:
 41             if cuncu == [] :
 42                 cuncu.append('0')
 43             cuncu.append('.')
 44             vartext.set(''.join(cuncu))
 45     def yunshuan(self):
 46         global cuncu, vartext, result, fuhao
 47         if vartext.get() == '':
 48             pass
 49         else:
 50             get1 = decimal.Decimal(vartext.get())
 51             if self.anjian in ('1/x','sqrt'):
 52                 if self.anjian == '1/x':
 53                     result = 1/get1
 54                 elif self.anjian == 'sqrt':
 55                     result = math.sqrt(get1)
 56             elif  self.anjian in ('+','-','*','/','='):
 57                 if fuhao is not None:
 58                     get1 = decimal.Decimal(result)
 59                     get2 = decimal.Decimal(vartext.get())
 60                     if fuhao == '+':
 61                         result = get1 + get2
 62                     elif fuhao == '-':
 63                         result = get1 - get2
 64                     elif fuhao == '*':
 65                         result = get1 * get2
 66                     elif fuhao == '/':
 67                         result = get1 / get2
 68                 else:
 69                     result = get1
 70                 if self.anjian == '=':
 71                     fuhao = None
 72                 else:
 73                     fuhao = self.anjian
 74             print(fuhao)
 75             print(result)
 76             vartext.set(str(result))
 77             cuncu.clear()
 78 
 79 def copy1():
 80     # tkinter.Misc().clipboard_clear()
 81     tkinter.Misc().clipboard_append(string(vartext.get()))
 82 
 83 def buju(root):
 84     global cuncu, vartext, result, fuhao
 85     #显示界面
 86     entry1 = tkinter.Label(root, width=30, height=5, bg='white', anchor='se', textvariable=vartext)
 87     entry1.grid(row=0, columnspan=5)
 88     #第一行按钮
 89     buttonMC=tkinter.Button(root,text='MC',width=5)
 90     buttonMR=tkinter.Button(root,text='MR',width=5)
 91     buttonMS=tkinter.Button(root,text='MS',width=5)
 92     buttonM1=tkinter.Button(root,text='M+',width=5)
 93     buttonM2=tkinter.Button(root,text='M-',width=5)
 94     buttonMC.grid(row=1,column=0)
 95     buttonMR.grid(row=1,column=1)
 96     buttonMS.grid(row=1,column=2)
 97     buttonM1.grid(row=1,column=3)
 98     buttonM2.grid(row=1,column=4)
 99 
100     #第二行按钮
101     buttonJ=tkinter.Button(root,text='',width=5,command=anjianzhi('c').tui)
102     buttonCE=tkinter.Button(root,text='CE',width=5)
103     buttonC=tkinter.Button(root,text=' C ',width=5,command=anjianzhi('c').clear)
104     button12=tkinter.Button(root,text='±',width=5,command=anjianzhi('c').zhengfu)
105     buttonD=tkinter.Button(root,text='',width=5,command=anjianzhi('sqrt').yunshuan)
106     buttonJ.grid(row=2,column=0)
107     buttonCE.grid(row=2,column=1)
108     buttonC.grid(row=2,column=2)
109     button12.grid(row=2,column=3)
110     buttonD.grid(row=2,column=4)
111     #第三行按钮
112     button7=tkinter.Button(root,text=' 7 ',width=5,command=anjianzhi('7').jia)
113     button8=tkinter.Button(root,text=' 8 ',width=5,command=anjianzhi('8').jia)
114     button9=tkinter.Button(root,text=' 9 ',width=5,command=anjianzhi('9').jia)
115     buttonc=tkinter.Button(root, text=' / ',width=5,command=anjianzhi('/').yunshuan)
116     buttonf= tkinter.Button(root, text=' % ',width=5)
117     button7.grid(row=3,column=0)
118     button8.grid(row=3,column=1)
119     button9.grid(row=3,column=2)
120     buttonc.grid(row=3,column=3)
121     buttonf.grid(row=3,column=4)
122     #第四行按钮
123     button4=tkinter.Button(root,text=' 4 ',width=5,command=anjianzhi('4').jia)
124     button5=tkinter.Button(root,text=' 5 ',width=5,command=anjianzhi('5').jia)
125     button6=tkinter.Button(root,text=' 6 ',width=5,command=anjianzhi('6').jia)
126     buttonx=tkinter.Button(root,text=' * ',width=5,command=anjianzhi('*').yunshuan)
127     buttonfs=tkinter.Button(root,text='1/x',width=5,command=anjianzhi('1/x').yunshuan)
128     button4.grid(row=4,column=0)
129     button5.grid(row=4,column=1)
130     button6.grid(row=4,column=2)
131     buttonx.grid(row=4,column=3)
132     buttonfs.grid(row=4,column=4)
133     #第五行按钮
134     button1 = tkinter.Button(root, text=' 1 ',width=5,command=anjianzhi('1').jia)
135     button2 = tkinter.Button(root, text=' 2 ',width=5,command=anjianzhi('2').jia)
136     button3 = tkinter.Button(root, text=' 3 ',width=5,command=anjianzhi('3').jia)
137     button_= tkinter.Button(root, text=' - ',width=5,command=anjianzhi('-').yunshuan)
138     buttondy= tkinter.Button(root, text=' \n = \n ',width=5,command=anjianzhi('=').yunshuan)
139     button1.grid(row=5, column=0)
140     button2.grid(row=5, column=1)
141     button3.grid(row=5, column=2)
142     button_.grid(row=5, column=3)
143     buttondy.grid(row=5, column=4,rowspan=2)
144     #第六行按钮
145     button0=tkinter.Button(root,text='   0   ',width=11,command=anjianzhi('0').jia)
146     buttonjh = tkinter.Button(root,text=' . ',width=5,command=anjianzhi('c').xiaoshudian)
147     buttonjia=tkinter.Button(root,text=' + ',width=5,command=anjianzhi('+').yunshuan)
148     button0.grid(row=6,column=0,columnspan=2)
149     buttonjh.grid(row=6,column=2)
150     buttonjia.grid(row=6,column=3)
151 def caidan(root): #菜单
152 
153     menu=tkinter.Menu(root)
154     submenu1=tkinter.Menu(menu,tearoff=0)
155     menu.add_cascade(label='查看',menu=submenu1)
156     submenu2 = tkinter.Menu(menu, tearoff=0)
157     submenu2.add_command(label='复制')
158     submenu2.add_command(label='粘贴')
159     menu.add_cascade(label='编辑',menu=submenu2)
160     submenu = tkinter.Menu(menu, tearoff=0)
161     submenu.add_command(label='查看帮助')
162     submenu.add_separator()
163     submenu.add_command(label='关于计算机')
164     menu.add_cascade(label='帮助',menu=submenu)
165     root.config(menu=menu)
166 
167 
168 buju(root)
169 Caidan (root)
 170 root.mainloop ()

The file Caculater.py into the D drive: D: //Caculater.py

Anacoda promt content switching to the D drive, input pyinstaller -D -w Caculater.py generated dist / test file directory, run inside the exe file, as shown in

 

Guess you like

Origin www.cnblogs.com/wenqiang-leo/p/11610634.html