鱼C论坛

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

[已解决]tkinter创建顶层窗口之后 按钮自动运行,求助

[复制链接]
发表于 2016-12-13 22:32:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想做一个简单的记账程序,可是生成顶层窗口之后,这个提交按钮为什么自己就运行,没有点他就运行指定的函数。求解
截图.png
  1. import tkinter as tk
  2. import time
  3. root=tk.Tk()
  4. root.geometry('1000x500+10+10')
  5. root.title('佳仔小程序')

  6. sell=tk.PhotoImage(file='sell.gif')
  7. test=tk.PhotoImage(file='test.gif')

  8. ISOTIMEFORRMAT='%Y %m %d %X'
  9. date=time.strftime(ISOTIMEFORRMAT)
  10. date_value=tk.StringVar()
  11. date_value.set(date)

  12. def sell_commodity(root):#商品的下拉列表
  13.     commodity_list=['棒棒糖','棉花糖','跳跳糖']
  14.     v=tk.StringVar(root)
  15.     v.set('请选择一个商品')
  16.     commodity_opm=tk.OptionMenu(root,v,*commodity_list)
  17.     commodity_opm.grid(row=2,column=2)
  18.     return v.get()
  19. def sell_huoyu(root,commodity):
  20.     tk.Label(root,text='(这个库里还有%s件)'%commodity).grid(row=2,column=3)
  21.     return commodity
  22. def sell_howmuch(root):
  23.     money=tk.IntVar()
  24.     money.set(123)
  25.     money_frame=tk.Entry(root,textvariable=money)
  26.     money_frame.grid(row=3,column=2)
  27.     return money.get()
  28. def sell_number(root):
  29.     num=tk.IntVar()
  30.     num.set(20)
  31.     num_frame=tk.Entry(root,textvariable=num)
  32.     num_frame.grid(row=4,column=2)
  33.     number=num.get()
  34.     return number
  35. def sell_money(root,howmuch,number):
  36.     money=tk.IntVar()
  37.     money.set(howmuch*number)   
  38.     sell_money_frame=tk.Entry(root,textvariable=money)
  39.     sell_money_frame.grid(row=5,column=2)
  40.     return money.get()
  41. def sell_man(root):
  42.     man=tk.StringVar()
  43.     man.set('请输入客户名')
  44.     man_frame=tk.Entry(root,textvariable=man)
  45.     man_frame.grid(row=6,column=2)
  46.     return man.get()
  47. def sell_after(root,befroe,num):
  48.     after_huoyu=int(befroe)-int(num)
  49.     tk.Label(root,text=after_huoyu).grid(row=7,column=2)
  50. def sell_check(man,time,howmuch,number,commodity,money):
  51.     print('%s在%s以%s的价格,购买了%s个%s,总共%s元'%(man,time,howmuch,number,commodity,money))
  52. def create():#创建顶层窗口
  53.     top=tk.Toplevel()
  54.     top.geometry('1000x500+10+10')
  55.     top.title('赚钱了赚钱了')
  56.     tk.Label(top,text='出售的日期时间为:').grid(row=0,column=1)
  57.     tk.Entry(top,textvariable=date_value).grid(row=0,column=2)
  58.     irow=2
  59.     for each in ['商品名称:','售价:','数量:','金额:','客户名:','售后库余:']:
  60.         tk.Label(top,text=each,anchor=tk.E).grid(row=irow,column=1)
  61.         irow+=1
  62.     #商品下拉列表   #库余label
  63.     commodity=sell_commodity(top)
  64.     huoyu=sell_huoyu(top,commodity=123)
  65.     #价格entry
  66.     howmuch=sell_howmuch(top)
  67.     #数量entry
  68.     number=sell_number(top)
  69.     #金额entry
  70.     money=sell_money(top,howmuch,number)
  71.     #客户entry
  72.     man=sell_man(top)
  73.     #售后库余label
  74.     if type(number)==int:
  75.         sell_after(top,huoyu,number)
  76.     #提交按钮Button
  77.     check=tk.Button(top,text='提交',\
  78.                     command=sell_check(man,date_value.get(),howmuch,number,commodity,money))
  79.     check.grid(row=8,column=2)
  80. l1=tk.Button(root,image=sell,command=create)
  81. l1.pack(fill =tk. Y, side = tk.LEFT)
  82. l2=tk.Button(root,image=test,command=create)
  83. l2.pack(fill=tk.Y,side=tk.RIGHT)



  84.    
  85.    
  86. root.mainloop()
复制代码



最佳答案
2016-12-14 14:26:04

def cmd_selchk():
    sell_check(man,date_value.get(),howmuch,number,commodity,money)
def create():#创建顶层窗口
     ……
    #提交按钮Button
    check=tk.Button(top,text='提交',
                    command=cmd_selchk)[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-12-14 14:26:04 | 显示全部楼层    本楼为最佳答案   

def cmd_selchk():
    sell_check(man,date_value.get(),howmuch,number,commodity,money)
def create():#创建顶层窗口
     ……
    #提交按钮Button
    check=tk.Button(top,text='提交',
                    command=cmd_selchk)[/code]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-12-14 16:09:50 | 显示全部楼层
66666
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-24 11:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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