python的gui编程(如何用 Python 写一个带 GUI 的科学计算程序)

:暂无数据 2026-05-16 03:40:01 1

python的gui编程(如何用 Python 写一个带 GUI 的科学计算程序)

大家好,python的gui编程相信很多的网友都不是很明白,包括如何用 Python 写一个带 GUI 的科学计算程序也是一样,不过没有关系,接下来就来为大家分享关于python的gui编程和如何用 Python 写一个带 GUI 的科学计算程序的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

本文目录

如何用 Python 写一个带 GUI 的科学计算程序

这是个代码, 使用Tkinter图形库,如果你是用的linux系统 记得将第一行改为from tkinter import *

这个代码实现的挺简单,并不是很复杂的科学计算器界面,你可以以此为基础,添加自己想要的东西:给你个截图:

代码是如下, 我就不给你添注释了啊:

#!/usr/bin/env python3.4
from Tkinter import *
import parser
root = Tk()
root.title(’Calculator’)
i = 0
def factorial():
    """Calculates the factorial of the number entered."""
    whole_string = display.get()
    number = int(whole_string)
    fact = 1
    counter = number 
    try:
        while counter 》 0:
            fact = fact*counter
            counter -= 1
        clear_all()
        display.insert(0, fact)
    except Exception:
        clear_all()
        display.insert(0, "Error")
def clear_all():
    """clears all the content in the Entry widget"""
    display.delete(0, END)
def get_variables(num):
    """Gets the user input for operands and puts it inside the entry widget"""
    global i
    display.insert(i, num)
    i += 1
def get_operation(operator):
    """Gets the operand the user wants to apply on the functions"""
    global i
    length = len(operator)
    display.insert(i, operator)
    i += length
def undo():
    """removes the last entered operator/variable from entry widget"""
    whole_string = display.get()
    if len(whole_string):        ## repeats until
        ## now just decrement the string by one index
        new_string = whole_string
        print(new_string)
        clear_all()
        display.insert(0, new_string)
    else:
        clear_all() 
        display.insert(0, "Error, press AC")
def calculate():
    """
    Evaluates the expression
***隐藏网址***
    """
    whole_string = display.get()
    try:
        formulae = parser.expr(whole_string).compile()
        result = eval(formulae)
        clear_all()
        display.insert(0, result)
    except Exception:
        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)
# adding new operations
pi = Button(root, text = "pi", command = lambda: get_operation("*3.14"), font =("Calibri", 12))
pi.grid(row = 2, column = 4)
modulo = Button(root, text = "%", command = lambda :  get_operation("%"), font=("Calibri", 12))
modulo.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)
## To be added :
# 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()

Python中simpleguitk的作用是什么

simpleguitk是一个Python库,用于创建简单的图形用户界面(GUI)。它提供了一些基本的组件,如按钮、文本框、标签等,使得开发者能够轻松地构建交互式的应用程序。

OK,关于python的gui编程和如何用 Python 写一个带 GUI 的科学计算程序的内容到此结束了,希望对大家有所帮助。

python的gui编程(如何用 Python 写一个带 GUI 的科学计算程序)

本文编辑:admin

更多文章:


send out用法及例句(send out是什么意思)

send out用法及例句(send out是什么意思)

各位老铁们好,相信很多人对send out用法及例句都不是特别的了解,因此呢,今天就来为大家分享下关于send out用法及例句以及send out是什么意思的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

2026年9月7日 20:30

全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)

全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)

大家好,关于全球新冠肺炎疫情背景下航运发展很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决

2026年9月7日 17:10

matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)

matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)

大家好,关于matlab求解带字母参数方程组很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数的知识点,相信应该可以解决大家的一些困惑和问题,

2026年9月7日 16:30

oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)

oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)

本篇文章给大家谈谈oracle中的循环语句,以及下面哪个不是oracle程序设计中的循环语句 a for对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

2026年9月7日 15:30

多表查询怎么做(Excel技巧:如何多表同步筛选)

多表查询怎么做(Excel技巧:如何多表同步筛选)

各位老铁们好,相信很多人对多表查询怎么做都不是特别的了解,因此呢,今天就来为大家分享下关于多表查询怎么做以及Excel技巧:如何多表同步筛选的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

2026年9月7日 15:10

服装店网站网页设计(服装网站建设目标设计要看甲方)

服装店网站网页设计(服装网站建设目标设计要看甲方)

“服装店网站网页设计”相关信息最新大全有哪些,这是大家都非常关心的,接下来就一起看看服装店网站网页设计(服装网站建设目标设计要看甲方)!

2026年9月7日 14:10

电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)

电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)

大家好,电脑里2个系统怎么删除一个相信很多的网友都不是很明白,包括电脑开机显示有两个系统,如何删除一个也是一样,不过没有关系,接下来就来为大家分享关于电脑里2个系统怎么删除一个和电脑开机显示有两个系统,如何删除一个的一些知识点,大家可以关注

2026年9月7日 12:20

苹果手机代码大全(恢复更新固件iOS未知错误代码解决大全)

苹果手机代码大全(恢复更新固件iOS未知错误代码解决大全)

大家好,今天小编来为大家解答以下的问题,关于苹果手机代码大全,恢复更新固件iOS未知错误代码解决大全这个很多人还不知道,现在让我们一起来看看吧!

2026年9月7日 10:20

scrollthrough意思(“scroll”是什么意思)

scrollthrough意思(“scroll”是什么意思)

今天给各位分享“scroll”是什么意思的知识,其中也会对“scroll”是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

2026年9月7日 08:00

怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)

怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)

本篇文章给大家谈谈怎么激活keygen,以及注册机如何激活cad2008一个简单激活cad2008的方法对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站

2026年9月7日 06:30

最近更新

热门文章

dessert(dessert是什么意思)
2026-05-03 12:45:03 浏览:7
标签列表