zoukankan      html  css  js  c++  java
  • python工具

    使用python自带的tkinter库进行GUI编程,完成两个功能:

    (1)要求用户输入姓名和年龄然后打印出来

    (2)要求用户输入一个数字,然后计算1到该数字之间的和

    代码部分:

    # 导入tkinter的所有的包里面所有的内容
    from tkinter import *
    
    import tkinter.messagebox as messagebox
    
    # 从Frame派生一个Application类,这是所有Widget的父容器
    class Application(Frame):
    
        def __init__(self,master=None):
            Frame.__init__(self,master)
            self.pack()
            self.createWidgets()
    
        def createWidgets(self):
    
            # 模块1,设定用户输入一个字符,alert弹框输出hello+该字符
            self.helloLabel = Label(self, text='模块1:请输入您的姓名及年龄,程序将会打印出来')  #Label为标签控件, 可以显示文本和位图
            self.helloLabel.pack()  # pack()方法把Widget加入到父容器中,并实现布局。
    
            self.nameInput = Entry(self)  #Entry为输入控件, 用于显示简单的文本内容
            self.nameInput.pack()
            self.ageInput = Entry(self)
            self.ageInput.pack()
    
            self.alertButton = Button(self,text='提交',command=self.hello)  # Button为按钮控件,在程序中显示按钮
            self.alertButton.pack()
    
            # 模块2,设定用户输入一个数字,alert弹框计算该数字的倍数
            self.helloLabel = Label(self, text='模块2:输入任意数字后将计算1到该数字之间的和')  # Text控件 , 文本控制用于显示多行文本
            self.helloLabel.pack()  # pack()方法把Widget加入到父容器中,并实现布局。
    
            self.numberInput = Entry(self)
            self.numberInput.pack()
    
            self.alertButton = Button(self,text='提交',command=self.sum)
            self.alertButton.pack()
    
            # 退出Button设定
            self.quitButton = Button(self, text='退出', command=self.quit)
            self.quitButton.pack()
    
        def hello(self):
            name = self.nameInput.get() or 'world'  # 获取用户输入的内容
            age = self.ageInput.get() or 20
            messagebox.showinfo('个人信息','姓名:%s
    年龄:%s岁' % (name,age))   # 调用用户输入的内容并打印出来
    
        def sum(self):
            number = int(self.numberInput.get())    # 获取用户输入的内容
    
            sum = 0
            for i in range(number):
                i += 1
                sum += i
            messagebox.showinfo('求和结果','1到%s之间的和为%s' % (number,sum)) # 调用用户输入的(数字 * 2)后并打印出来
    
    # 实例化
    app = Application()
    
    # 设置窗口标题:
    app.master.title('Hello World')
    
    # 主消息循环
    app.mainloop()

    执行效果:

  • 相关阅读:
    Components controls 区别
    lazarus 2016 2月18 4:22:35 支持android开发了, 既ios,linux,macosx,window,web 后 囊括一切啦。 哈哈
    Delphi MlSkin V1.1 发布啦! 它能让你的程序拥有像QQ一样多彩炫丽的外观!
    Tclientdataset的CloneCursor问题 clientdataset 复制 赋值 的问题
    字符串 映射 函数
    字符串 映射相应的 函数 字符串驱动技术—— MethodAddress , MethodName , ObjectInvoke
    一分钟了解nohup和&的功效
    Linux shell标准输入,标准输出,错误输出
    (一)shell编程之执行脚本的三种方式
    Shell:执行脚本文件方法
  • 原文地址:https://www.cnblogs.com/tdcqma/p/6905666.html
Copyright © 2011-2022 走看看