猫史档案馆


Python【编程小讲堂】第十期 Tkinter高级实战2

用户:爱因斯坦的弟子爱因斯坦的弟子查看:3 回复:2 评论:3 创建时间:2019-08-20T11:54:54


2、按钮  

这个有点类似于  Qt- 的信号-槽函数 的关系,点击button触发到事件。

import Tkinter
import tkMessageBox

top = Tkinter.Tk()

def helloCallBack():
tkMessageBox.showinfo( "Hello Python", "Hello Runoob")

B = Tkinter.Button(top, text ="点我", command = helloCallBack)

B.pack()

top.mainloop()

3、标签 & 输入框

from tkinter import *

top = Tk()
L1 = Label(top, text="网站名")
L1.pack(side=LEFT)
E1 = Entry(top, bd=5)
E1.pack(side=RIGHT)

top.mainloop()

%%提示:这是本人的全资料汇总,更正了其他帖子的各种语法错误

1、翻译器

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from tkinter import *
#import tkMessageBox
import requests

# 定义button事件
def translation():
	content = entry.get()
	if content == '':
		tkMessageBox.showinfo("提示", "请输入需要翻译的内容")
	else:
		print (content)
	header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KH喵L, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
	url = "http://fanyi.youdao.com/translate?喵artresult=dict&喵artresult=rule"
	# url = "http://fanyi.youdao.com/translate_o?喵artresult=dict&喵artresult=rule"
	data = {}
	data['i'] = content
	data['from'] = 'AUTO'
	data['to'] = 'AUTO'
	data['喵artresult'] = 'dict'
	data['client'] = 'fanyideskweb'
	# data['salt'] = '1530332652360' # 时间戳
	# data['sign'] = '7f281888e41d12a63b20790707672708'
	data['doctype'] = 'json'
	data['version'] = '2.1'
	data['keyfrom'] = 'fanyi.web'
	data['action'] = 'FY_BY_CLICKBUTTION'
	data['typoResult'] = 'false'
	result = requests.post(url,data=data,headers=header)
	translation = result.json()
	translation = translation['translateResult'][0][0]['tgt']
	print(translation)
	res.set(translation)


root = Tk()
#1、窗口标题

res = StringVar()

root.title('中英文互译')
#2、窗口大小,显示位置
root.geometry('290x100+573+286')
#3、标签控件
lable = Label(root,text='输入要翻译的文字',font=('微软雅黑',10), fg='black')
lable.grid()

lable = Label(root,text='翻译结果',font=('微软雅黑',10), fg='black')
lable.grid()

#4、输入控件
entry=Entry(root, font='微软雅黑')
entry.grid(row=0,column=1)
#显示翻译之后的内容
entry2=Entry(root, font='微软雅黑', textvariable=res)
#输入标签位置
entry2.grid(row=1,column=1)

#5、按钮控件
button = Button(root, text ="翻译",width=10,font=('微软雅黑',10),command=translation)
button.grid(row=2,column=0,sticky=W)

button = Button(root, text ="退出",width=10,font=('微软雅黑',10), command=root.quit)
button.grid(row=2,column=1,sticky=E)

#-----------------至此,界面已经画出来了---------------------------

root.mainloop()

 


回复

上一页1 页 / 共 1下一页
520669已退猫520669已退猫

顶一顶

点赞0


评论


爱因斯坦的弟子爱因斯坦的弟子

本期链接集合(高中函数)

https://shequ.codemao.cn/community/192143

https://shequ.codemao.cn/community/192145

https://shequ.codemao.cn/community/192149

点赞0


评论