用python tkinter写的xml-rpc客户端...
时间:2010-08-17 来源:huzhenwei
这个程序还有一点小bug——点击list_method按钮时,列出的任务按钮会不断增加。
# coding: utf-8 from Tkinter import Tk, Toplevel, Button, Label, Checkbutton, IntVar, Menu, Entry, \ Text, Frame, StringVar, END, Radiobutton, LabelFrame import xmlrpclib xmlrpc_url = 'http://localhost:8888/' root = Tk() method_name = StringVar() #method_name.set('system.methodHelp') url = StringVar() url.set(xmlrpc_url) def connect_server(): global proxy proxy = xmlrpclib.ServerProxy(url_entry.get()) def append_text(msg): result_text.insert(END, '%s\n' % msg) def list_method(): methods = proxy.system.listMethods() for i, method in enumerate(methods): exec "radio%d = Radiobutton(method_frame, variable=method_name, value='%s()', text='%s')" % (i, method, method) exec "radio%d.pack()" % i def show_text(): #print result_text.get('1.0') result_text.insert(END, 'append\n') def sumbit_request(): try: exec "result = proxy.%s" % request_method.get() except Exception, e: print e append_text(e) else: append_text(result) #url_frame框架中包含‘输入XML-RPC server url‘的单行文本控件和‘连接XML-RPC server’的按钮 url_frame = Frame(root, bg='red', height=40) url_frame.pack() #method_frame框架包含'xml-rpc服务可用的方法'的按钮控件 method_frame = LabelFrame(root, text='method', width=50, height=200, bg='blue') method_frame.pack(anchor='w') #request_frame框架包含'发送请求到xml-rpc服务器'的按钮控件 request_frame = LabelFrame(root, text='request', width=50, height=200, bg='yellow') request_frame.pack(anchor='e') #result_frame框架包含‘显示程序运行结果’的多行文本控件 result_frame = Frame(root, bg='green') #result_frame.pack(side='bottom') result_frame.pack() #‘输入XML-RPC server url‘的单行文本控件 url_entry = Entry(url_frame, textvariable=url, width=100) url_entry.pack() #url_entry["textvariable"] = contents #‘连接XML-RPC server’的按钮 url_sumbit = Button(url_frame, text='connect', command=connect_server) url_sumbit.pack() #'列出XML-RPC server上实现的方法'的按钮 list_method_button = Button(method_frame, text='list_method', command=list_method) list_method_button.pack() #‘发送到XML-RPC server的函数名和参数’的单行文本控件 request_method = Entry(request_frame, textvariable=method_name, width=50) request_method.pack() #‘发送请求到XML-RPC server’的按钮 request_sumbit = Button(request_frame, text='sumbit', command=sumbit_request) request_sumbit.pack() #‘显示程序运行结果’的多行文本控件 result_text = Text(result_frame, width=100) result_text.pack() root.mainloop()
运行时的图片如下: