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

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 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 关于#单片机#的问题:以ATMEGA128或相近型号单片机为控制器设计直流电机调速的闭环控制系统(相关搜索:设计报告|软件设计|流程图)
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框
  • ¥15 苹果验机结果的api接口哪里有??单次调用1毛钱及以下。
  • ¥20 学生成绩管理系统设计