为什么拆分方法停止使用TKINTER?

发布于 2025-02-08 11:58:23 字数 746 浏览 2 评论 0原文

每当我使用concped = first_word.split()行运行此代码时,我会收到一个错误(窗口立即关闭)。

import tkinter as tk

win = tk.Tk()
win.title("Conversation")
win.iconbitmap("cake.ico")
win.geometry("600x700")
#Lists
Hellos = ["greetings", 'hello', 'greetings', 'hi']
gday = ['good', 'great', 'incredible', 'not bad', 'okay']
bday = ['bad', 'awful', 'not the best', 'terrible']

fw_label = tk.Label(win, text="Hello user, it's nice to meet you.")
fw_label.pack()
first_word = tk.Entry()
first_word.pack()
chopped = first_word.split()

但是,当我更改行first_word = tk.entry() first_word =“ a normal String” 时,拆分方法突出显示,当我悬停时,它给出了其描述, ````first_word = tk.entry()''没有发生。

使用OpenCV之类的库时,我遇到了这个问题,我可以知道是什么原因导致它不起作用?

Whenever I run this code with the chopped = first_word.split() line I get an error (the window closes instantly).

import tkinter as tk

win = tk.Tk()
win.title("Conversation")
win.iconbitmap("cake.ico")
win.geometry("600x700")
#Lists
Hellos = ["greetings", 'hello', 'greetings', 'hi']
gday = ['good', 'great', 'incredible', 'not bad', 'okay']
bday = ['bad', 'awful', 'not the best', 'terrible']

fw_label = tk.Label(win, text="Hello user, it's nice to meet you.")
fw_label.pack()
first_word = tk.Entry()
first_word.pack()
chopped = first_word.split()

But when I change the line first_word = tk.Entry() to first_word="A normal string" , the split method highlights and when I hover it it gives its description, which wasn't happening with ```first_word = tk.Entry()``.

I've ran into this problem when using libraries like opencv, may I know what's causing it not to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北风几吹夏 2025-02-15 11:58:23

解决方案很简单。按下提交按钮时,您必须调用一个函数。语句conced = first_word.get()。split()应该在该功能内部。

到了错误部分,这是因为first_word是一个entribute object object split> split()。
拆分方法仅适用于字符串。要以字符串的形式将文本输入到条目小部件中,您需要使用get()方法...

import tkinter as tk

def click():
    chopped = first_word.get().split()
    print(chopped)

win = tk.Tk()
win.title("Conversation")
# win.iconbitmap("cake.ico")
win.geometry("600x700")
#Lists
Hellos = ["greetings", 'hello', 'greetings', 'hi']
gday = ['good', 'great', 'incredible', 'not bad', 'okay']
bday = ['bad', 'awful', 'not the best', 'terrible']

fw_label = tk.Label(win, text="Hello user, it's nice to meet you.")
fw_label.pack()
first_word = tk.Entry()
first_word.pack()
tk.Button(win, text="Submit", command=click).pack()

win.mainloop()

因此,您使用get()获取条目文本方法,然后将其分开

注意:语句chopped = first_word.get()。split()应该在函数内部,因为如果其外部,它将在其外部执行创建条目小部件并使用get()将导致空值,因为该条目当时不包含任何内容

The solution is simple. You have to make a function to be called while pressing the submit button. And the statement chopped = first_word.get().split() should be inside that function.

Coming to the error part, it was because first_word is a tkinter entry object without any attribute split().
The split method works only for strings. To get the text entered into the entry widget in form of string, you need to use the get() method...

import tkinter as tk

def click():
    chopped = first_word.get().split()
    print(chopped)

win = tk.Tk()
win.title("Conversation")
# win.iconbitmap("cake.ico")
win.geometry("600x700")
#Lists
Hellos = ["greetings", 'hello', 'greetings', 'hi']
gday = ['good', 'great', 'incredible', 'not bad', 'okay']
bday = ['bad', 'awful', 'not the best', 'terrible']

fw_label = tk.Label(win, text="Hello user, it's nice to meet you.")
fw_label.pack()
first_word = tk.Entry()
first_word.pack()
tk.Button(win, text="Submit", command=click).pack()

win.mainloop()

So, you get the text of the entry using the get() method and then split it

Note : The statement chopped = first_word.get().split() should be inside the function because if its outside it, it would be executed at the time of the creation of the entry widget and using get() there would result in an empty value as the entry doesn't contain anything at that point

ヤ经典坏疍 2025-02-15 11:58:23

也许这可以

改变

chopped = first_word.split()

chopped = first_word.get().split()

Maybe this could work

Change

chopped = first_word.split()

to

chopped = first_word.get().split()
尴尬癌患者 2025-02-15 11:58:23

由于声誉较低,我无法回答我对您的最后一个问题的评论。 https://www.shapedivider.app/在这里您可以轻松地制作所需的曲线并将代码嵌入到项目中。我经常使用。

I'm unable to reply my comment on your last question because of my low reputation. https://www.shapedivider.app/ here you would easily make the curves you want and embed the code to your project. I use this often.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文