|
4鱼币
本帖最后由 红唇如刀 于 2018-7-12 16:50 编辑
为什么 打开路径 按钮按下去就谈不起来了
- import tkinter.filedialog
- from tkinter import *
- import tkinter
- import os
- import sys
- stockList=[]
- def openPath(event):
- file_open=tkinter.filedialog.askopenfilename(title=u'选择文件')
- if len(file_open) !=0:
- file_read=open(file_open)
- str_path.set(file_open)
- line=file_read.read()
- line=line.split('\n')
-
- for each in line:
- if each[0]=='1':
- each=',sh'+each[1:]
- else:
- each=',sz'+each[1:]
- stockList.append(each)
- file_read.close()
- else:
- pass
-
- def listAddtotxt(event):
- file_write=open(r'C:\Users\Administrator\Desktop\测试.txt','a')
- for each in stockList:
- file_write.write(each)
-
- file_write.close()
- str_result.set('本次共生成代码 {0} 家'.format (len(stockList)))
-
- win =Tk()
- win['height']=100
- win['width']=600
- win.title('利用通达信板块导出的txt生成代码表')
- label_name=Label(win,text='板块txt文件:')
- label_name.place(x=5,y=16,height=30)
- label_code=Label(win,text='证券代码表:')
- label_code.place(x=5,y=52,height=30)
- button_open=Button(win,text='打开路径')#创建按钮
- button_open.place(x=75,y=16,height=30)#设置位置
- button_open.bind('<Button-1>',openPath)#绑定事件
- button_addto=Button(win,text='批量生成')
- button_addto.place(x=75,y=52,height=30)
- button_addto.bind('<Button-1>',listAddtotxt)
- str_path=StringVar()
- entry_path=Entry(win,textvariable=str_path)
- entry_path.place(x=143,y=16,height=30,width=446)
- entry_path.focus()
- entry_path.bind('<Return>'or '<KP_Enter>',listAddtotxt)
- str_result=StringVar() #创建一个传值对象用来传递结果输入框的值
- entry_result=Entry(win,textvariable=str_result)#创建结果输入框
- entry_result.place(x=143,y=52,height=30,width=446)#结果输入框的位置
- mainloop()
复制代码
请大侠出手相助
- import tkinter.filedialog
- from tkinter import *
- import tkinter
- import os
- import sys
- stockList=[]
- def openPath():
- file_open=tkinter.filedialog.askopenfilename(title=u'选择文件')
- if len(file_open) !=0:
- file_read=open(file_open)
- str_path.set(file_open)
- line=file_read.read()
- line=line.split('\n')
-
- for each in line:
- if each[0]=='1':
- each=',sh'+each[1:]
- else:
- each=',sz'+each[1:]
- stockList.append(each)
- file_read.close()
- else:
- pass
-
- def listAddtotxt():
- file_write=open(r'C:\Users\Administrator\Desktop\测试.txt','a')
- for each in stockList:
- file_write.write(each)
-
- file_write.close()
- str_result.set('本次共生成代码 {0} 家'.format (len(stockList)))
-
- win =Tk()
- win['height']=100
- win['width']=600
- win.title('利用通达信板块导出的txt生成代码表')
- label_name=Label(win,text='板块txt文件:')
- label_name.place(x=5,y=16,height=30)
- label_code=Label(win,text='证券代码表:')
- label_code.place(x=5,y=52,height=30)
- button_open=Button(win,text='打开路径',command=openPath)#创建按钮
- button_open.place(x=75,y=16,height=30)#设置位置
- #button_open.bind('<Button-1>',openPath)#绑定事件
- button_addto=Button(win,text='批量生成',command=listAddtotxt)
- button_addto.place(x=75,y=52,height=30)
- #button_addto.bind('<Button-1>',listAddtotxt)
- str_path=StringVar()
- entry_path=Entry(win,textvariable=str_path)
- entry_path.place(x=143,y=16,height=30,width=446)
- entry_path.focus()
- entry_path.bind('<Return>'or '<KP_Enter>',listAddtotxt)
- str_result=StringVar() #创建一个传值对象用来传递结果输入框的值
- entry_result=Entry(win,textvariable=str_result)#创建结果输入框
- entry_result.place(x=143,y=52,height=30,width=446)#结果输入框的位置
- mainloop()
复制代码
绑定事件直接在创建按钮的时候就可以用command=function绑定
还有,上面那两个函数,没有传入event这个参数
只是说解决了弹不起来的办法,至于有没有影响到功能,大概没有吧。。。
|
最佳答案
查看完整内容
绑定事件直接在创建按钮的时候就可以用command=function绑定
还有,上面那两个函数,没有传入event这个参数
只是说解决了弹不起来的办法,至于有没有影响到功能,大概没有吧。。。
|