A. 如何用 python 寫一個帶 GUI 的科學計算程序
這是個代碼, 使用Tkinter圖形庫,如果你是用的linux系統 記得將第一行改為from tkinter import *
這個代碼實現的挺簡單,並不是很復雜的科學計算器界面,你可以以此為基礎,添加自己想要的東西:給你個截圖:
#!/usr/bin/envpython3.4
fromTkinterimport*
importparser
root=Tk()
root.title('Calculator')
i=0
deffactorial():
"""."""
whole_string=display.get()
number=int(whole_string)
fact=1
counter=number
try:
whilecounter>0:
fact=fact*counter
counter-=1
clear_all()
display.insert(0,fact)
exceptException:
clear_all()
display.insert(0,"Error")
defclear_all():
""""""
display.delete(0,END)
defget_variables(num):
""""""
globali
display.insert(i,num)
i+=1
defget_operation(operator):
""""""
globali
length=len(operator)
display.insert(i,operator)
i+=length
defundo():
"""removesthelastenteredoperator/variablefromentrywidget"""
whole_string=display.get()
iflen(whole_string):##repeatsuntil
##
new_string=whole_string[:-1]
print(new_string)
clear_all()
display.insert(0,new_string)
else:
clear_all()
display.insert(0,"Error,pressAC")
defcalculate():
"""
Evaluatestheexpression
ref:http://stackoverflow.com/questions/594266/equation-parsing-in-python
"""
whole_string=display.get()
try:
formulae=parser.expr(whole_string).compile()
result=eval(formulae)
clear_all()
display.insert(0,result)
exceptException:
clear_all()
display.insert(0,"Error!")
root.columnconfigure(0,pad=3)
root.columnconfigure(1,pad=3)
root.columnconfigure(2,pad=3)
root.columnconfigure(3,pad=3)
root.columnconfigure(4,pad=3)
root.rowconfigure(0,pad=3)
root.rowconfigure(1,pad=3)
root.rowconfigure(2,pad=3)
root.rowconfigure(3,pad=3)
display=Entry(root,font=("Calibri",13))
display.grid(row=1,columnspan=6,sticky=W+E)
one=Button(root,text="1",command=lambda:get_variables(1),font=("Calibri",12))
one.grid(row=2,column=0)
two=Button(root,text="2",command=lambda:get_variables(2),font=("Calibri",12))
two.grid(row=2,column=1)
three=Button(root,text="3",command=lambda:get_variables(3),font=("Calibri",12))
three.grid(row=2,column=2)
four=Button(root,text="4",command=lambda:get_variables(4),font=("Calibri",12))
four.grid(row=3,column=0)
five=Button(root,text="5",command=lambda:get_variables(5),font=("Calibri",12))
five.grid(row=3,column=1)
six=Button(root,text="6",command=lambda:get_variables(6),font=("Calibri",12))
six.grid(row=3,column=2)
seven=Button(root,text="7",command=lambda:get_variables(7),font=("Calibri",12))
seven.grid(row=4,column=0)
eight=Button(root,text="8",command=lambda:get_variables(8),font=("Calibri",12))
eight.grid(row=4,column=1)
nine=Button(root,text="9",command=lambda:get_variables(9),font=("Calibri",12))
nine.grid(row=4,column=2)
cls=Button(root,text="AC",command=clear_all,font=("Calibri",12),foreground="red")
cls.grid(row=5,column=0)
zero=Button(root,text="0",command=lambda:get_variables(0),font=("Calibri",12))
zero.grid(row=5,column=1)
result=Button(root,text="=",command=calculate,font=("Calibri",12),foreground="red")
result.grid(row=5,column=2)
plus=Button(root,text="+",command=lambda:get_operation("+"),font=("Calibri",12))
plus.grid(row=2,column=3)
minus=Button(root,text="-",command=lambda:get_operation("-"),font=("Calibri",12))
minus.grid(row=3,column=3)
multiply=Button(root,text="*",command=lambda:get_operation("*"),font=("Calibri",12))
multiply.grid(row=4,column=3)
divide=Button(root,text="/",command=lambda:get_operation("/"),font=("Calibri",12))
divide.grid(row=5,column=3)
#addingnewoperations
pi=Button(root,text="pi",command=lambda:get_operation("*3.14"),font=("Calibri",12))
pi.grid(row=2,column=4)
molo=Button(root,text="%",command=lambda:get_operation("%"),font=("Calibri",12))
molo.grid(row=3,column=4)
left_bracket=Button(root,text="(",command=lambda:get_operation("("),font=("Calibri",12))
left_bracket.grid(row=4,column=4)
exp=Button(root,text="exp",command=lambda:get_operation("**"),font=("Calibri",10))
exp.grid(row=5,column=4)
##Tobeadded:
#sin,cos,log,ln
undo_button=Button(root,text="<-",command=undo,font=("Calibri",12),foreground="red")
undo_button.grid(row=2,column=5)
fact=Button(root,text="x!",command=factorial,font=("Calibri",12))
fact.grid(row=3,column=5)
right_bracket=Button(root,text=")",command=lambda:get_operation(")"),font=("Calibri",12))
right_bracket.grid(row=4,column=5)
square=Button(root,text="^2",command=lambda:get_operation("**2"),font=("Calibri",10))
square.grid(row=5,column=5)
root.mainloop()
B. 可以用易語言設計gui,python寫代碼嗎
理論上是可以的,可以嵌入一個python的解釋器來被調用運行python的代碼。
但是直接用python 設計GUI現寫代碼不是更好么。
python有多個GUI庫的。
通常python自帶一個簡易的TKinter庫,做個簡單的窗口應用就夠了。
wxPython是Python語言的另一套優秀的GUI圖形庫
PyQt是一個創建GUI應用程序的工具包。它是Python編程語言和Qt庫的成功融合。Qt庫是目前最強大的庫之一。