python tkinter模块 创建窗口V1.2

先上图

代码如下

  1 #-*-coding:utf-8-*-
  2 import os
  3 from tkinter import *
  4 
  5 root=Tk()
  6 root.title('执行窗口')
  7 
  8 """
  9 V1.2
 10 
 11 """
 12 def add(a,b):
 13     a=int(a)
 14     b=int(b)
 15     sum=eval('a+b')  #执行表达式
 16     print('a+b=',sum)
 17 
 18     return sum
 19 
 20 #调用其他函数fun(asg1,asg2)
 21 def go_func():
 22     #第一个参数,调用函数,第2个参数,函数参数
 23     func = n51.get()
 24     asgs = n52.get()
 25     func = eval(func)  #转为函数
 26     #print(type(func))    
 27     #无参数
 28     if len(asgs) == 0:
 29         func()
 30     else:
 31         #多参数j,分割
 32         asg=asgs.split(',')
 33         func(*asg)
 34 
 35 #--------------------------------------
 36 #清空文本框内容
 37 def  clear():
 38     text.delete('1.0','end')
 39     
 40 #执行
 41 def show():
 42     print('hello')
 43     text.insert('1.0', "hello\n")
 44 
 45 #文本信息
 46 def insert_text(msg):
 47     text.insert(INSERT,'%s'%msg)
 48 
 49 def get_app_deviceid():    
 50     print('请先检查是否连接了设备,是否启动了开发者选项,是否开启了adb调试....')
 51     #查看连接设备
 52     out=os.popen('adb devices').read()
 53     patter= re.compile(r"[a-zA-Z0-9]+") 
 54     device_list=patter.findall(out)
 55     #print(device_list)
 56     print('设备连接信息:--------------------------------------\n',out)
 57 
 58     #调取text
 59     insert_text(out)
 60     
 61     #存放设备号
 62     deviceid=[]
 63     #提取设备号,存放到deviceid中,
 64     if 'device' in device_list:
 65         #print('设备号:',deviceid)
 66         #多个设备,
 67         n=4
 68         while len(device_list)>n:
 69             deviceid.append(device_list[n])
 70             n=n+2
 71         print('设备号:',deviceid)        
 72     else:
 73         print('无此设备,请检查是否连接设备。')
 74     return out
 75 
 76 
 77 #执行cmd命令
 78 def  execute_cmd(cmd='adb devices'):
 79         cmd= e1.get()
 80         print(cmd)
 81         out = os.popen(cmd).read()
 82         print(out)
 83         return out
 84 
 85 def get_text():
 86     # 获取entry输入的文字
 87     str2=""
 88     str2=e1.get()
 89     
 90     # 在光标处插入文字
 91     text.insert("insert", str2)
 92     
 93 #-grid-----------------------------------------------------------------------
 94 
 95 #label控件
 96 Label(root,text="输入命令:").grid(row=0,column=0,sticky=E) #靠右
 97 Label(root,text="本地目录:").grid(row=1,column=0,sticky=E) #靠右
 98 Label(root, text='手机目录').grid(row=2,column=0,sticky=E) #靠左
 99 
100 Label(root, text='显示结果',width=15).grid(row=3,column=1,sticky=W) #靠左
101 
102 #输入控件
103 e1=Entry(root,width=30)
104 e1.grid(row=0,column=1,padx=5,pady=5)
105 e2=Entry(root,width=30)
106 e2.grid(row=1,column=1,padx=5,pady=5)
107 e3=Entry(root,width=30)
108 e3.grid(row=2,column=1,padx=5,pady=5)
109 
110 
111 #命令控件
112 #b1=Button(root,text="点击看看吧",command=show,height=1,width=15,fg='blue').grid(row=2,column=1)    
113 #Label(root, text='显示结果', width=15, height=1).grid(row=2,column=1,sticky=W) #靠左
114 #grid(row=1,column=2),row,行,从0开始,column列从0开始;
115 b1=Button(root,text="执行命令",command=get_text,height=1,width=15,fg='blue').grid(row=0,column=2,padx=5, pady=5)
116 b2=Button(root,text="安装包",command=show,height=1,width=15,fg='blue').grid(row=1,column=2,padx=5, pady=5)
117 
118 b3=Button(root,text="查看设备",command=get_app_deviceid,height=1,width=15,fg='blue').grid(row=4,column=0,padx=5, pady=5)
119 b4=Button(root,text="清空",command=clear,height=1,width=15,fg='black').grid(row=4,column=2,padx=5, pady=5)
120 
121 
122 
123 #显示结果,text控件
124 text = Text(root, width=30, font =('Verdana',10),fg='blue')
125 text.grid(row=4,column=1,rowspan=3)
126 
127 
128 #调用函数
129 Label(root, text='调用函数').grid(row=8,column=0) 
130 Label(root, text='输入函数名').grid(row=8,column=1) 
131 Label(root, text='输入参数').grid(row=8,column=2) 
132 
133 #调用函数
134 b5= Button(root,text = "加法",command = go_func,width = 15).grid(row = 9,column = 0,padx=5, pady=5)
135 #输入函数名
136 var1 = StringVar()
137 n51 = Entry(root,width = 30,textvariable = var1)
138 var1.set("add")
139 n51.grid(row = 9,column = 1)
140 
141 #输入函数参数
142 var2 = StringVar()
143 n52 = Entry(root,width = 30,textvariable = var2)
144 var2.set("a,b")
145 n52.grid(row = 9,column = 2)
146 
147 
148 root.mainloop()

猜你喜欢

转载自www.cnblogs.com/lisa2016/p/10855877.html
今日推荐