python - Python Tkinter 文本小部件中的随机 { }
问题描述
在 Python 中,我使用 Tkinter 并为机器人制作了一个文本小部件以显示随机日期。但由于某种原因,文本小部件在文本中显示随机 { }。我无法弄清楚是什么导致所有 { } 出现。 这是一个截图
执行此操作的代码部分在这里:
self.text_widget = Text(self.ui, width=20, height=2, bg="#f1f1f1", fg="#656565",
font=Font1, padx=5, pady=5)
self.text_widget.place(relheight=0.845, relwidth=1, rely=0.08)
self.text_widget.configure(cursor="arrow", state=DISABLED)
# Create Random Date
year = random.randrange(2021, 2121)
monthd = random.randrange(1, 12)
if monthd == 1:
month = "January"
if monthd == 2:
month = "Febuary"
if monthd == 3:
month = "March"
if monthd == 4:
month = "April"
if monthd == 5:
month = "May"
if monthd == 6:
month = "June"
if monthd == 7:
month = "July"
if monthd == 8:
month = "August"
if monthd == 9:
month = "September"
if monthd == 10:
month = "October"
if monthd == 11:
month = "November"
if monthd == 12:
month = "December"
date = random.randrange(1, 29)
answer = ""
answer = "on " , date , ", " , month , year
# Display Date
msg2 = "Bot: " , answer , "\n\n\n"
self.text_widget.configure(state=NORMAL)
self.text_widget.insert(END, msg2)
self.text_widget.configure(state=DISABLED)
print("[OK] Self Message Displayed\n")
self.text_widget.see(END)
有谁知道如何摆脱 {} ?
解决方案
你的问题似乎是
msg2 = "Bot: " , answer , "\n\n\n"
因为它创建了一个元组。
只需将其更改为
msg2 = "Bot: " + answer + "\n\n\n"
我还注意到另一个问题
monthd = random.randrange(1, 12)
只会返回 1 到 11。
answer
有相同的问题msg2
,因此有相同的解决方案。
推荐阅读
- scala - 对猫的逆变函子应用、应用、Monad 等?
- sql - SQL中关于NVL的查询
- c# - 日期格式的正则表达式
- python - 从 QTableView 单元格 DisplayRole 填充上下文菜单
- ios - Alamofire 根据输入更改多部分/表单数据编码
- javascript - AttributeError: 'list' 对象没有使用 Selenium 和 Python 的属性 'click'
- ios - 带有搜索栏动画故障的大标题
- c# - C# MVVM,等待一些事件触发并继续更新 UI
- javascript - JavaScript 中的 `async/await` 中的 `return timer` 和 `clearInterval(timer)` 有什么区别?
- java - 使用 testrail api 在 java 中创建测试运行