鱼C论坛

 找回密码
 立即注册
查看: 2705|回复: 2

[已解决]为什么我的按钮不弹起?????????

[复制链接]
发表于 2018-6-30 11:23:57 | 显示全部楼层 |阅读模式
4鱼币
本帖最后由 红唇如刀 于 2018-7-12 16:50 编辑

为什么 打开路径 按钮按下去就谈不起来了

为什么 打开路径 按钮按下去就谈不起来了


  1. import tkinter.filedialog
  2. from tkinter import *
  3. import tkinter
  4. import os
  5. import sys

  6. stockList=[]
  7. def openPath(event):
  8.     file_open=tkinter.filedialog.askopenfilename(title=u'选择文件')
  9.     if len(file_open) !=0:
  10.         file_read=open(file_open)
  11.         str_path.set(file_open)
  12.         line=file_read.read()
  13.         line=line.split('\n')
  14.    
  15.         for each in line:
  16.             if each[0]=='1':
  17.                 each=',sh'+each[1:]        
  18.             else:
  19.                 each=',sz'+each[1:]
  20.             stockList.append(each)
  21.         file_read.close()
  22.     else:
  23.             pass
  24.         
  25. def listAddtotxt(event):
  26.     file_write=open(r'C:\Users\Administrator\Desktop\测试.txt','a')
  27.     for each in stockList:        
  28.         file_write.write(each)
  29.         
  30.     file_write.close()   
  31.     str_result.set('本次共生成代码 {0} 家'.format (len(stockList)))
  32.    
  33. win =Tk()
  34. win['height']=100
  35. win['width']=600
  36. win.title('利用通达信板块导出的txt生成代码表')

  37. label_name=Label(win,text='板块txt文件:')
  38. label_name.place(x=5,y=16,height=30)        
  39. label_code=Label(win,text='证券代码表:')   
  40. label_code.place(x=5,y=52,height=30)        

  41. button_open=Button(win,text='打开路径')#创建按钮
  42. button_open.place(x=75,y=16,height=30)#设置位置
  43. button_open.bind('<Button-1>',openPath)#绑定事件

  44. button_addto=Button(win,text='批量生成')
  45. button_addto.place(x=75,y=52,height=30)
  46. button_addto.bind('<Button-1>',listAddtotxt)

  47. str_path=StringVar()
  48. entry_path=Entry(win,textvariable=str_path)
  49. entry_path.place(x=143,y=16,height=30,width=446)
  50. entry_path.focus()
  51. entry_path.bind('<Return>'or '<KP_Enter>',listAddtotxt)

  52. str_result=StringVar()    #创建一个传值对象用来传递结果输入框的值
  53. entry_result=Entry(win,textvariable=str_result)#创建结果输入框
  54. entry_result.place(x=143,y=52,height=30,width=446)#结果输入框的位置

  55. mainloop()
复制代码


请大侠出手相助

最佳答案
2018-6-30 11:23:58
  1. import tkinter.filedialog
  2. from tkinter import *
  3. import tkinter
  4. import os
  5. import sys

  6. stockList=[]
  7. def openPath():
  8.     file_open=tkinter.filedialog.askopenfilename(title=u'选择文件')
  9.     if len(file_open) !=0:
  10.         file_read=open(file_open)
  11.         str_path.set(file_open)
  12.         line=file_read.read()
  13.         line=line.split('\n')
  14.    
  15.         for each in line:
  16.             if each[0]=='1':
  17.                 each=',sh'+each[1:]        
  18.             else:
  19.                 each=',sz'+each[1:]
  20.             stockList.append(each)
  21.         file_read.close()
  22.     else:
  23.             pass
  24.         
  25. def listAddtotxt():
  26.     file_write=open(r'C:\Users\Administrator\Desktop\测试.txt','a')
  27.     for each in stockList:        
  28.         file_write.write(each)
  29.         
  30.     file_write.close()   
  31.     str_result.set('本次共生成代码 {0} 家'.format (len(stockList)))
  32.    
  33. win =Tk()
  34. win['height']=100
  35. win['width']=600
  36. win.title('利用通达信板块导出的txt生成代码表')

  37. label_name=Label(win,text='板块txt文件:')
  38. label_name.place(x=5,y=16,height=30)        
  39. label_code=Label(win,text='证券代码表:')   
  40. label_code.place(x=5,y=52,height=30)        

  41. button_open=Button(win,text='打开路径',command=openPath)#创建按钮
  42. button_open.place(x=75,y=16,height=30)#设置位置
  43. #button_open.bind('<Button-1>',openPath)#绑定事件

  44. button_addto=Button(win,text='批量生成',command=listAddtotxt)
  45. button_addto.place(x=75,y=52,height=30)
  46. #button_addto.bind('<Button-1>',listAddtotxt)

  47. str_path=StringVar()
  48. entry_path=Entry(win,textvariable=str_path)
  49. entry_path.place(x=143,y=16,height=30,width=446)
  50. entry_path.focus()
  51. entry_path.bind('<Return>'or '<KP_Enter>',listAddtotxt)

  52. str_result=StringVar()    #创建一个传值对象用来传递结果输入框的值
  53. entry_result=Entry(win,textvariable=str_result)#创建结果输入框
  54. entry_result.place(x=143,y=52,height=30,width=446)#结果输入框的位置

  55. mainloop()
复制代码



绑定事件直接在创建按钮的时候就可以用command=function绑定
还有,上面那两个函数,没有传入event这个参数
只是说解决了弹不起来的办法,至于有没有影响到功能,大概没有吧。。。

最佳答案

查看完整内容

绑定事件直接在创建按钮的时候就可以用command=function绑定 还有,上面那两个函数,没有传入event这个参数 只是说解决了弹不起来的办法,至于有没有影响到功能,大概没有吧。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-6-30 11:23:58 | 显示全部楼层    本楼为最佳答案   
  1. import tkinter.filedialog
  2. from tkinter import *
  3. import tkinter
  4. import os
  5. import sys

  6. stockList=[]
  7. def openPath():
  8.     file_open=tkinter.filedialog.askopenfilename(title=u'选择文件')
  9.     if len(file_open) !=0:
  10.         file_read=open(file_open)
  11.         str_path.set(file_open)
  12.         line=file_read.read()
  13.         line=line.split('\n')
  14.    
  15.         for each in line:
  16.             if each[0]=='1':
  17.                 each=',sh'+each[1:]        
  18.             else:
  19.                 each=',sz'+each[1:]
  20.             stockList.append(each)
  21.         file_read.close()
  22.     else:
  23.             pass
  24.         
  25. def listAddtotxt():
  26.     file_write=open(r'C:\Users\Administrator\Desktop\测试.txt','a')
  27.     for each in stockList:        
  28.         file_write.write(each)
  29.         
  30.     file_write.close()   
  31.     str_result.set('本次共生成代码 {0} 家'.format (len(stockList)))
  32.    
  33. win =Tk()
  34. win['height']=100
  35. win['width']=600
  36. win.title('利用通达信板块导出的txt生成代码表')

  37. label_name=Label(win,text='板块txt文件:')
  38. label_name.place(x=5,y=16,height=30)        
  39. label_code=Label(win,text='证券代码表:')   
  40. label_code.place(x=5,y=52,height=30)        

  41. button_open=Button(win,text='打开路径',command=openPath)#创建按钮
  42. button_open.place(x=75,y=16,height=30)#设置位置
  43. #button_open.bind('<Button-1>',openPath)#绑定事件

  44. button_addto=Button(win,text='批量生成',command=listAddtotxt)
  45. button_addto.place(x=75,y=52,height=30)
  46. #button_addto.bind('<Button-1>',listAddtotxt)

  47. str_path=StringVar()
  48. entry_path=Entry(win,textvariable=str_path)
  49. entry_path.place(x=143,y=16,height=30,width=446)
  50. entry_path.focus()
  51. entry_path.bind('<Return>'or '<KP_Enter>',listAddtotxt)

  52. str_result=StringVar()    #创建一个传值对象用来传递结果输入框的值
  53. entry_result=Entry(win,textvariable=str_result)#创建结果输入框
  54. entry_result.place(x=143,y=52,height=30,width=446)#结果输入框的位置

  55. mainloop()
复制代码



绑定事件直接在创建按钮的时候就可以用command=function绑定
还有,上面那两个函数,没有传入event这个参数
只是说解决了弹不起来的办法,至于有没有影响到功能,大概没有吧。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-12 17:10:00 | 显示全部楼层
haxfs 发表于 2018-7-12 17:07
绑定事件直接在创建按钮的时候就可以用command=function绑定
还有,上面那两个函数,没有传入event ...

爱你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 15:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表