鱼C论坛

 找回密码
 立即注册
查看: 1509|回复: 3

[已解决]为何tkinter 在我写的函数中无法直接使用

[复制链接]
发表于 2020-8-29 20:59:01 | 显示全部楼层 |阅读模式

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

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

x
"""
checkbutton 练习
"""

from tkinter import *


# 普通版
def normalButton():
    root = Tk()

    v = IntVar()

    c = Checkbutton(root, text='test1', variable=v)
    c.pack()

    l = Label(root, textvariable=v)
    l.pack()

    return root.mainloop()


# 循环版
def circleButton():
    root = Tk()

    GIRLS = ['1', '2', '3', '4']

    v = []

    for girl in GIRLS:
        v.append(IntVar())
        b = Checkbutton(root, text=girl, variable=v[-1])
        b.pack()

    root.mainloop()
    return

runCode = input('press 1 or 2')

if runCode==1:
    normalButton()
elif runCode==2:
    circleButton()


运行后发现tkinter没有弹出任何组件程序就结束了。这是为啥。
最佳答案
2020-8-29 21:04:13
本帖最后由 1q23w31 于 2020-8-29 21:10 编辑

看这个,因为input接收到的是字符型变量,只有变为整型变量后,才能与数字比较大小
  1. from tkinter import *


  2. # 普通版
  3. def normalButton():
  4.     root = Tk()

  5.     v = IntVar()

  6.     c = Checkbutton(root, text='test1', variable=v)
  7.     c.pack()

  8.     l = Label(root, textvariable=v)
  9.     l.pack()

  10.     return root.mainloop()
  11. def circleButton():
  12.     root = Tk()

  13.     GIRLS = ['1', '2', '3', '4']

  14.     v = []

  15.     for girl in GIRLS:
  16.         v.append(IntVar())
  17.         b = Checkbutton(root, text=girl, variable=v[-1])
  18.         b.pack()

  19.     root.mainloop()
  20.     return

  21. runCode = int(input('press 1 or 2'))

  22. if runCode==1:
  23.     normalButton()
  24. elif runCode==2:
  25.     circleButton()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-29 21:04:13 | 显示全部楼层    本楼为最佳答案   
本帖最后由 1q23w31 于 2020-8-29 21:10 编辑

看这个,因为input接收到的是字符型变量,只有变为整型变量后,才能与数字比较大小
  1. from tkinter import *


  2. # 普通版
  3. def normalButton():
  4.     root = Tk()

  5.     v = IntVar()

  6.     c = Checkbutton(root, text='test1', variable=v)
  7.     c.pack()

  8.     l = Label(root, textvariable=v)
  9.     l.pack()

  10.     return root.mainloop()
  11. def circleButton():
  12.     root = Tk()

  13.     GIRLS = ['1', '2', '3', '4']

  14.     v = []

  15.     for girl in GIRLS:
  16.         v.append(IntVar())
  17.         b = Checkbutton(root, text=girl, variable=v[-1])
  18.         b.pack()

  19.     root.mainloop()
  20.     return

  21. runCode = int(input('press 1 or 2'))

  22. if runCode==1:
  23.     normalButton()
  24. elif runCode==2:
  25.     circleButton()
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
feiua + 5 + 5 + 3 感谢秒回

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

发表于 2020-8-29 21:08:16 | 显示全部楼层
1q23w31 发表于 2020-8-29 21:04
看这个,因为input接收到的是字符型变量,只有变为整型变量后,才能与数字比较大小

  1. runCode = iint(nput('press 1 or 2'))
复制代码

多打了个字母
  1. runCode = int(nput('press 1 or 2'))
复制代码


最好还是改下面的判断为字符,万一输入的不是数字,int()不是会报错么
  1. runCode = input('press 1 or 2')

  2. if runCode=='1':
  3.     normalButton()
  4. elif runCode=='2':
  5.     circleButton()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-29 21:09:33 | 显示全部楼层

谢谢提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 13:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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