请用Python给我写一个程序,程序要求:1:使用Tkinter图形化操作界面,需要创建桌面应用程序界面,图形化操作界面长为500,宽为500;2:程序拥有计算器功能,可以计算三角函数,通过点击“=”可得出答案;3:程序拥有查询国内所有省、市、直辖市、自治区、自治州、自治县、区、县、村、特区实时天气的功能,可通过在指定的输入框内输入城市名称来查询,并把查询到的信息通过文字形式呈现在图形化操作界面上;4:程序可通过点击“退出”按钮来退出程序;5:程序具有访问网页的功能,可通过在指定输入框内输入网址来实现访问网页。
时间: 2023-06-03 15:04:13 浏览: 104
基于python的Tkinter实现一个简易计算器
好的,根据您的要求,下面给出Python代码实现:
```python
import tkinter as tk
import math
import requests
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.geometry('500x500')
self.master.title('Desktop Application')
self.create_widgets()
def create_widgets(self):
# label for calculator
self.label_cal = tk.Label(self.master, text='Calculator', font=('Arial', 16))
self.label_cal.place(x=50, y=50)
# input box for calculator
self.input_cal = tk.Entry(self.master, width=30)
self.input_cal.place(x=50, y=100)
# buttons for calculator
self.button_sin = tk.Button(self.master, text='sin', width=7, command=lambda:self.insert_cal('sin('))
self.button_sin.place(x=50, y=150)
self.button_cos = tk.Button(self.master, text='cos', width=7, command=lambda:self.insert_cal('cos('))
self.button_cos.place(x=100, y=150)
self.button_tan = tk.Button(self.master, text='tan', width=7, command=lambda:self.insert_cal('tan('))
self.button_tan.place(x=150, y=150)
self.button_square = tk.Button(self.master, text='^2', width=7, command=lambda:self.insert_cal('**2'))
self.button_square.place(x=200, y=150)
self.button_root = tk.Button(self.master, text='√', width=7, command=lambda:self.insert_cal('math.sqrt('))
self.button_root.place(x=250, y=150)
self.button_left = tk.Button(self.master, text='(', width=7, command=lambda:self.insert_cal('('))
self.button_left.place(x=300, y=150)
self.button_right = tk.Button(self.master, text=')', width=7, command=lambda:self.insert_cal(')'))
self.button_right.place(x=350, y=150)
self.button_equal = tk.Button(self.master, text='=', width=7, command=self.calculate)
self.button_equal.place(x=400, y=150)
# label for weather query
self.label_weather = tk.Label(self.master, text='Weather Query', font=('Arial', 16))
self.label_weather.place(x=50, y=250)
# input box for weather query
self.input_weather = tk.Entry(self.master, width=30)
self.input_weather.place(x=50, y=300)
# button for weather query
self.button_query = tk.Button(self.master, text='Query', width=7, command=self.query_weather)
self.button_query.place(x=150, y=350)
# label for weather result
self.label_result = tk.Label(self.master, font=('Arial', 12), justify='left', anchor='w', wraplength=400)
self.label_result.place(x=50, y=380)
# button for website access
self.label_website = tk.Label(self.master, text='Website Access', font=('Arial', 16))
self.label_website.place(x=50, y=430)
# input box for website access
self.input_website = tk.Entry(self.master, width=30)
self.input_website.place(x=50, y=470)
# button for website access
self.button_go = tk.Button(self.master, text='Go', width=7, command=self.access_website)
self.button_go.place(x=150, y=510)
# button for exit
self.button_exit = tk.Button(self.master, text='Exit', width=7, command=self.master.quit)
self.button_exit.place(x=400, y=550)
def insert_cal(self, text):
self.input_cal.insert(tk.END, text)
def calculate(self):
expression = self.input_cal.get()
try:
result = eval(expression)
self.input_cal.delete(0, tk.END)
self.input_cal.insert(tk.END, result)
except Exception as e:
self.input_cal.delete(0, tk.END)
self.input_cal.insert(tk.END, 'Error: ' + str(e))
def query_weather(self):
city = self.input_weather.get()
url = f'http://wthrcdn.etouch.cn/weather_mini?city={city}'
try:
response = requests.get(url).json()
data = response['data']
forecast = data['forecast'][0]
weather = forecast['type']
temperature = forecast['high'] + '/' + forecast['low']
message = f'City: {city}\nWeather: {weather}\nTemperature: {temperature}'
self.label_result.config(text=message)
except Exception as e:
self.label_result.config(text='Error: ' + str(e))
def access_website(self):
url = self.input_website.get()
try:
response = requests.get(url)
self.label_result.config(text=response.text)
except Exception as e:
self.label_result.config(text='Error: ' + str(e))
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
这个程序实现了您的要求,包括了图形化界面、计算器功能、天气查询功能、访问网页功能和退出程序功能。您可以直接把它复制到Python IDE中运行。
阅读全文