python输出姓名_python小工具 - alert弹框输出姓名年龄、求和

# 导入tkinter的所有的包里面所有的内容from tkinter import *import tkinter.messageboxasmessagebox

# 从Frame派生一个Application类,这是所有Widget的父容器classApplication(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:请输入您的姓名及年龄,程序将会打印出来‘)

self.helloLabel.pack() # pack()方法把Widget加入到父容器中,并实现布局。

self.nameInput=Entry(self)

self.nameInput.pack()

self.ageInput=Entry(self)

self.ageInput.pack()

self.alertButton= Button(self,text=‘提交‘,command=self.hello)

self.alertButton.pack()

# 模块2,设定用户输入一个数字,alert弹框计算该数字的倍数

self.helloLabel= Label(self, text=‘模块2:输入任意数字后将计算1到该数字之间的和‘)

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 20messagebox.showinfo(‘个人信息‘,‘姓名:%s\n年龄:%s岁‘ %(name,age)) # 调用用户输入的内容并打印出来

def sum(self):

number= int(self.numberInput.get()) # 获取用户输入的内容

sum= 0

for i inrange(number):

i+= 1sum+=i

messagebox.showinfo(‘求和结果‘,‘1到%s之间的和为%s‘ % (number,sum)) # 调用用户输入的(数字 * 2)后并打印出来

# 实例化

app=Application()

# 设置窗口标题:

app.master.title(‘Hello World‘)

# 主消息循环

app.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值