代码作用:使用Tkinter绘制界面并设置IP和端口,开启本地的http服务
注意:代码使用Tkinter,为python2版本。使用Tkinter 来绘制界面,进行IP和端口 的设置。不足之处没有写代码进程的终止。也就是没有写停止服务。
# coding:utf-8
print u'服务开启中......'
from Tkinter import *
from threading import Thread
from serverfuwu import server123
import socket
import ttk
import tkMessageBox
import time
def show_info():
"""show the software info"""
tkMessageBox.showinfo("错误",
"""
错误提示
------------------------------------------
请重新输入您的IP地址和端口号!!!
------------------------------------------""")
def show_info1():
"""show the software info"""
tkMessageBox.showinfo("用法",
"""
工具助手
------------------------------------------
请点击自动获取或输入您的IP地址,
然后输入您的端口号,点击开启服务,
即可在浏览器进行下载.
eg:http://192.168.1.104:8080
------------------------------------------""")
def printhelp():
tl = Toplevel()
tl.title('usage')
tl.geometry('300x200')
tl.resizable(width= False,height=False)
Label(tl, text='请输入您的IP或者点击自动获取来\n得到您的IP地址,然后输入您的端口号,\n点击开启服务器即可.\n\n\n 谢谢',
font = 15).pack()
def getIP():
list1 = []
addrs = socket.getaddrinfo(socket.gethostname(), None)
for item in addrs:
list1.append(item[4][0])
list2 = tuple(list1)
return list2
def printIP():
if numberChosen.get() and c2.get():
IP = numberChosen.get()
PORT = int(c2.get())
server123(IP,PORT)
else:
show_info()
def sevicego():
thr1 = Thread(target=printIP)
thr1.start()
def localIP():
thr = Thread(target=getIP)
thr.start()
if __name__ == "__main__":
root = Tk()
root.title('SERVER')
root.geometry('400x300')
root.resizable(height=False, width=False)
root.iconbitmap('login.ico')
menubar = Menu(root)
filemenu1 = Menu(root, bg='red')
filemenu2 = Menu(root, bg='red')
filemenu3 = Menu(root, bg='red')
menubar.add_cascade(label='菜单', menu=filemenu1)
menubar.add_cascade(label='工具', menu=filemenu2)
menubar.add_cascade(label='help', menu=filemenu3)
filemenu1.insert_command(1, label='暂无')
filemenu2.insert_command(1, label='暂无')
filemenu3.insert_command(1, label='用法', command=show_info1)
root['menu'] = menubar
Label(root,text='IP地址:',font=20).place(x=50, y=30)
number =StringVar()
numberChosen = ttk.Combobox(root, width=28, textvariable=number)
numberChosen['values'] = getIP() # 设置下拉列表的值
numberChosen.place(x=80, y=53) # 设置其在界面中出现的位置 column代表列 row 代表行
numberChosen.current(0) # 设置下拉列表默认显示的值,0为 numberChosen['values'] 的下标值
Label(root, text='端口号',font=20
).place(x=50, y=100)
c2 = StringVar()
entry = Entry(root, textvariable=c2, width=30)
entry.place(x=80, y=123)
Label(root, text='提示:端口号请在0-65535之间',font=15
).place(x=50, y=150)
Button(root,text='开启服务器',font=20,bg='blue',command=sevicego,
).place(x=130, y=200)
print u'服务初始化完成!'
print(u'当前主机名称为 : ' + socket.gethostname())
root.mainloop()
time.sleep(5)