gpt4 book ai didi

python - "TypeError: Can' t 将 'NoneType' 对象隐式转换为 str“当 var 应该有一个值时

转载 作者:太空宇宙 更新时间:2023-11-04 03:40:20 25 4
gpt4 key购买 nike

import sys
from tkinter import *
def print():
print("Encoded " + message + " with " + offset)

gui = Tk()

gui.title("Caesar Cypher Encoder")

Button(gui, text="Encode", command=encode).grid(row = 2, column = 2)
Label(gui, text = "Message").grid(row = 1, column =0)
Label(gui, text = "Offset").grid(row = 1, column =1)
message = Entry(gui).grid(row=2, column=0)
offset = Scale(gui, from_=0, to=25).grid(row=2, column=1)

mainloop( )

当我在输入框中同时输入一个值并在 slider 上输入一个值时运行此代码 - 它会出现错误

>>>Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:/Users/xxxx/Desktop/Code/Functionised/GUI.pyw", line 5, in encode
print("Encoded " + message + " with " + offset)
TypeError: Can't convert 'NoneType' object to str implicitly

顺便说一句,使用简单的 str() 不起作用

编辑

使用新代码

import sys
from tkinter import *
def printer():
print(message)
print(offset)

gui = Tk()

gui.title("Caesar Cypher Encoder")

Button(gui, text="Encode", command=printer).grid(row = 2, column = 2)
Label(gui, text = "Message").grid(row = 1, column =0)
Label(gui, text = "Offset").grid(row = 1, column =1)
message = Entry(gui)
message.grid(row=2, column=0)
offset = Scale(gui, from_=0, to=25)
offset.grid(row=2, column=1)

mainloop( )

返回

.46329264
.46329296

编辑 2

def printer():
print(message.get())
print(offset.get())

这解决了 .xxxxxxxx 问题

最佳答案

在回答您的第一次编辑时 - .get() 命令应该是最有用的

的形式使用
print(message.get())

而不是

print(message)

关于python - "TypeError: Can' t 将 'NoneType' 对象隐式转换为 str“当 var 应该有一个值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26822410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com