鱼C论坛

 找回密码
 立即注册
查看: 1656|回复: 18

[已解决]新手求助,关于tkinter entry

[复制链接]
发表于 2018-4-5 11:21:20 | 显示全部楼层 |阅读模式

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

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

x
e = Entry(root)
e.get()
global w
w = e.get()
请问这样w获得的数据是什么类型?entry输入的是数字整型,要怎么得到输入的数字整型?
我需要获取entry的输入值与数字整型比较
直接用w与数字整型比较 返回TypeError: '>' not supported between instances of 'str' and 'int'
用int(w)比较则返回ValueError: invalid literal for int() with base 10: ''

最佳答案
2018-4-5 11:58:18
xunuo9890 发表于 2018-4-5 11:51
我重新把代码整理下再把全代码发给你吧

你这里当没有输入的时候会将空字符串给int()
自己加条件判断吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-5 11:27:23 | 显示全部楼层
本帖最后由 塔利班 于 2018-4-5 11:30 编辑

e.get()得到字符串类型,要想比较用int()转换
你要确保e.get()输入没有添加进空格之类的东西
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:30:32 | 显示全部楼层
  1. eee
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:31:00 | 显示全部楼层
塔利班 发表于 2018-4-5 11:27
e.get()得到字符串类型,要想比较用int()转换

试过了啊
用int(w)比较返回ValueError: invalid literal for int() with base 10: ''
无法转换,不知道为什么
用int(e.get())则返回UnboundLocalError: local variable 'e' referenced before assignment
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:31:39 | 显示全部楼层
xunuo9890 发表于 2018-4-5 11:31
试过了啊
用int(w)比较返回ValueError: invalid literal for int() with base 10: ''
无法转换,不知道 ...

贴全你的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:32:28 | 显示全部楼层
xunuo9890 发表于 2018-4-5 11:31
试过了啊
用int(w)比较返回ValueError: invalid literal for int() with base 10: ''
无法转换,不知道 ...

无法转换的时候都是你输入了带空格或者其他字符
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:34:07 | 显示全部楼层
类型转换后比效
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:34:11 | 显示全部楼层
from tkinter import *
import sys
import random

def choiceboundary():

    def gameing():
        def conutinue():

            def game1():
                a = random.randint(0,11)
                b = int(e.get())
                c = int(0)
                d = int(2)
                while (b != a) and (c < d):
                    if b > a:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太大了!重新输入:').grid(row = 0)
                        e = Entry(root)
                        e.grid(row = 1,column = 0,padx = 10,pady = 5)
                        theButton = Button(root,text = '确认',width = 10,command = game1).grid(row = 2,column = 2,sticky = W,padx = 10,pady = 5)
                        c += 1
                        
                        mainloop()
                    if b < a:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太小了!重新输入:').grid(row = 0)
                        e = Entry(root)
                        e.grid(row = 1,column = 0,padx = 10,pady = 5)
                        theButton = Button(root,text = '确认',width = 10,command = game1).grid(row = 2,column = 2,sticky = W,padx = 10,pady = 5)
                        c += 1
                        mainloop()
                        
                if (b == a) and (c < d):
                   root = Tk()
                   textLabel = Label(root,text = '猜对了!')
                   textLabel.pack()
                   mainloop()
                if (c == d):
                   root = Tk()
                   textLabel = Label(root,text = '没有机会了!')
                   textLabel.pask()
                   mainloop()
      
            def game2():
                f = random.randint(0,31)
                g = int(e.get())
                h = int(0)
                i = int(3)
                while (g != f) and (h<i):
                    if g>f:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太大了!重新输入:'.grid(row = 0))
                        e = Entry(root)
                        e.grid(row = 1,column = 1,padx = 10,pady = 5)
                        mainloop()
                    if g<f:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太小了!重新输入:'.grid(row = 0))
                        e = Entry(root)
                        e.grid(row = 1,column = 1,padx = 10,pady = 5)
                        mainloop()
                    h += 1   
                if (g == f) and (h<i):
                   root = Tk()
                   textLabel = Label(root,text = '猜对了!')
                   textLabel.pack()
                   mainloop()
                if (h == i):
                   root = Tk()
                   textLabel = Label(root,text = '没有机会了!')
                   textLabel.pask()
                   mainloop()

            def game3():
                j = random.randint(0,51)
                k = int(e.get())
                l = int(0)
                o = int(5)
                while (k != j) and (l<o):
                    if k>j:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太大了!重新输入:'.grid(row = 0))
                        e = Entry(root)
                        e.grid(row = 1,column = 1,padx = 10,pady = 5)
                        mainloop()
                    if k<j:
                        root = Tk()
                        root.title('重新输入')
                        Label(root,text = '太小了!重新输入:'.grid(row = 0))
                        e = Entry(root)
                        e.grid(row = 1,column = 1,padx = 10,pady = 5)
                        mainloop()
                    l += 1   
                if (k == j) and (l<o):
                   root = Tk()
                   textLabel = Label(root,text = '猜对了!')
                   textLabel.pack()
                   mainloop()
                if (l == o):
                   root = Tk()
                   textLabel = Label(root,text = '没有机会了!')
                   textLabel.pask()
                   mainloop()

            
            if p1:
                game1()
            if p2:
                game2()
            if p3:
                game3()


        root = Tk()
        root.title('游戏进行中')
        Label(root,text = '输入你猜的数字:').grid(row = 0)
        e = Entry(root)
        e.grid(row = 0,column = 1,padx = 10,pady = 5)
        theButton = Button(root,text = '确认',width = 10,command = conutinue).grid(row = 2,column = 0,sticky = W,padx = 10,pady = 5)
        theButton = Button(root,text = '退出',width = 10,command = root.quit).grid(row = 2,column = 1,sticky = E,padx = 10,pady = 5)
        global w
        w = e.get()

        mainloop()




   
    root = Tk()
    root.title('猜数字游戏')
    group = LabelFrame(root,text = '选择难度',padx = 30,pady = 30)
    group.pack(padx = 50,pady = 50)
    v = IntVar()
    p1 = Radiobutton(group,text = '简单(0-10,2次机会)',variable = v,value = 1,indicatoron = False,command = gameing)
    p1.pack(fill = X)
    p2 = Radiobutton(group,text = '中等(0-30,3次机会)',variable = v,value = 2,indicatoron = False,command = gameing)
    p2.pack(fill = X)
    p3 = Radiobutton(group,text = '困难(0-50,5次机会)',variable = v,value = 3,indicatoron = False,command = gameing)
    p3.pack(fill = X)

    mainloop()


            

root = Tk()

root.title('XNgame')
photo = PhotoImage(file = 'XG.gif')
theLabel = Label(root,
                 text = '欢迎使用XNgame!',
                 justify = LEFT,
                 image = photo,
                 compound = CENTER,
                 font = ('微软雅黑',20),
                 fg='black'
                 )
theLabel.pack()
theButton = Button(root,text = '进入',command = choiceboundary)
theButton.pack()

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

使用道具 举报

 楼主| 发表于 2018-4-5 11:36:48 | 显示全部楼层
塔利班 发表于 2018-4-5 11:32
无法转换的时候都是你输入了带空格或者其他字符

全代码在下面
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:37:46 | 显示全部楼层
ba21 发表于 2018-4-5 11:34
类型转换后比效

用了b = int(e.get())
再比较是ValueError: invalid literal for int() with base 10: ''

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

使用道具 举报

发表于 2018-4-5 11:38:46 | 显示全部楼层
你的e还没定义就类型转换了,调整好顺序
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:41:02 | 显示全部楼层
xunuo9890 发表于 2018-4-5 11:37
用了b = int(e.get())
再比较是ValueError: invalid literal for int() with base 10: ''

哪来那么多
root = Tk()
mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:42:21 | 显示全部楼层
塔利班 发表于 2018-4-5 11:38
你的e还没定义就类型转换了,调整好顺序

有试过先定义
global w
w = e.get()
再用b = int(w)来比较
返回ValueError: invalid literal for int() with base 10: ''
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:45:13 | 显示全部楼层
ba21 发表于 2018-4-5 11:41
哪来那么多
root = Tk()
mainloop()

额分了好几个界面,之前在学校没得实战只能把代码写在纸上,为了层次分明点。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:46:25 | 显示全部楼层
你这函数结构也挺乱的,
我分析都头大
你用
b = int(w.strip())
试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:50:39 | 显示全部楼层
塔利班 发表于 2018-4-5 11:46
你这函数结构也挺乱的,
我分析都头大
你用

还是ValueError: invalid literal for int() with base 10: ''
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 11:51:58 | 显示全部楼层
xunuo9890 发表于 2018-4-5 11:50
还是ValueError: invalid literal for int() with base 10: ''

我重新把代码整理下再把全代码发给你吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-5 11:58:18 | 显示全部楼层    本楼为最佳答案   
xunuo9890 发表于 2018-4-5 11:51
我重新把代码整理下再把全代码发给你吧

你这里当没有输入的时候会将空字符串给int()
自己加条件判断吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-5 12:30:22 | 显示全部楼层
塔利班 发表于 2018-4-5 11:58
你这里当没有输入的时候会将空字符串给int()
自己加条件判断吧

是的发现了,我还没有输入e.get()就已经把空字符串当成输入结果给了w 捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-1 02:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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