Rheinmetals 2021-07-26 23:18 采纳率: 100%
浏览 1256
已结题

python tkinter 输不出结果”TypeError: 'str' object cannot be interpreted as an integer“

想要输出类似于”A112“的字符串所代表的储物架位置,但是发现函数无法使用

from tkinter import *
from tkinter import messagebox
text1=r=''
x=5
window = Tk()
window.title("First Window")
window.geometry("350x200")
lbl = Label(window, text="Hello")
lbl.grid(column=0, row=0)
txt = Entry(window, width=10)
txt.grid(column=1, row=0)
txt2=Label(window, text="output")
txt2.grid(column=1, row=1)
def searching():
    for i in range(r):
        south=["Z","Y","X","W","V","U","T","S","R","Q","P","O","M","N","L","K","J",]
        middle=["H"]
        north=["A","B","C","D","E","F","G"]
        x=input("请输入序列号(斜杠前字母大写)")
        a1=x[0]
        a2=x[1]
        if a1=='G':
            if '5' >= a2 >='0':
                text3 = "北架"
                txt2.configure(text=text3)
            else:
                text3="中架"
                txt2.configure(text=text3)
        elif a1=='I':
            text3='人工操作'
            txt2.configure(text=text3)
        elif a1 in south:
            text3="南架"
            txt2.configure(text=text3)      
        elif a1 in middle:
            text3="中架"
            txt2.configure(text=text3)
        elif a1 in north:
            text3="北架"
            txt2.configure(text=text3)
        else:
            text3="请再确认编号"
            txt2.configure(text=text3)
btn = Button(window, text="searching", command=searching)
btn.grid(column=2, row=0)
window.mainloop()

调用之后就提示”TypeError: 'str' object cannot be interpreted as an integer“
希望有朋友能够提供帮助!

  • 写回答

1条回答 默认 最新

  • 关注
          for i in range(r):
    

    这个r是空字符串啊。你想用for循环什么啊?
    如果不需要循环把 for i in range(r): 这行去掉就好


    你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

    from tkinter import *
    from tkinter import messagebox
    from tkinter.simpledialog import askstring
    
    text1=r=''
    x=5
    window = Tk()
    window.title("First Window")
    window.geometry("350x200")
    lbl = Label(window, text="Hello")
    lbl.grid(column=0, row=0)
    txt = Entry(window, width=10)
    txt.grid(column=1, row=0)
    txt2=Label(window, text="output")
    txt2.grid(column=1, row=1)
    def searching():
        south=["Z","Y","X","W","V","U","T","S","R","Q","P","O","M","N","L","K","J",]
        middle=["H"]
        north=["A","B","C","D","E","F","G"]
        x=askstring("输入序列号","请输入序列号(斜杠前字母大写)")
        a1=x[0]
        if a1=='G':
            a2=x[1]
            if '5' >= a2 >='0':
                text3 = "北架"
                txt2.configure(text=text3)
            else:
                text3="中架"
                txt2.configure(text=text3)
        elif a1=='I':
            text3='人工操作'
            txt2.configure(text=text3)
        elif a1 in south:
            text3="南架"
            txt2.configure(text=text3)
        elif a1 in middle:
            text3="中架"
            txt2.configure(text=text3)
        elif a1 in north:
            text3="北架"
            txt2.configure(text=text3)
        else:
            text3="请再确认编号"
            txt2.configure(text=text3)
    btn = Button(window, text="searching", command=searching)
    btn.grid(column=2, row=0)
    window.mainloop()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月4日
  • 已采纳回答 7月27日
  • 创建了问题 7月26日

悬赏问题

  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下: