[英]I want to print the entry text from textbox to console. Below is my code
I am trying to make a GUI using python tkinter. 我正在尝试使用python tkinter制作GUI。 I want to print the entry text from textbox to console after the analyzebutton is pressed.
我想在按下analytics按钮后将输入文本从文本框打印到控制台。 here is my code
这是我的代码
root = Tk()
root.title('Title')
MiddleFrame = Frame(root)
BottomFrame = Frame(root)
TopFrame.pack(side = TOP)
MiddleFrame.pack()
BottomFrame.pack(side = BOTTOM)
TextArea = Text()
ScrollBar = Scrollbar(root)
ScrollBar.config(command = TextArea.yview)
TextArea.config(height = 25, width = 70,
background = "white", yscrollcommand = ScrollBar.set)
TextArea.grid(padx = 18, pady = 18)
ScrollBar.pack(side = RIGHT, fill = Y)
padx = 10
pady = 10
TextArea.pack(padx = padx, pady = pady)
AnalyzeButton = Button(BottomFrame, text = "Analyze", fg = "white", bg = "blue", command = callback)
AnalyzeButton.pack(fill = X, padx = padx, pady = pady)
def callback():
text_input = Text.get()
print(text_input)
root.mainloop()
thanks in advance 提前致谢
Use get method of Text. 使用Text的get方法。 Use the widget not the class-Text.
使用小部件而不是class-Text。 Here is what you need to do:-
这是您需要做的:-
text_input = TextArea.get("1.0","end-1c")
You have several problems in your code. 您的代码中有几个问题。 I will break it down so you understand what is going on here.
我将对其进行分解,以便您了解此处的情况。
Fist I noticed you have tried to pack TopFrame
but you have not actually defined TopFrame
yet. 拳头我注意到您已经尝试打包
TopFrame
但实际上尚未定义TopFrame
。 So I added TopFrame = Frame(root)
to your code. 所以我在您的代码中添加了
TopFrame = Frame(root)
。
Next we have a common mistake people encounter when trying to use grid()
and pack()
on the same window/frame. 接下来,当人们尝试在同一窗口/框架上使用
grid()
和pack()
时,我们会遇到一个常见的错误。 This is not allowed by the geometry manager. 几何管理器不允许这样做。 So you will need to decide on either
grid()
or pack()
for all your needs in each window/frame. 因此,您将需要为每个窗口/框架中的所有需求选择
grid()
或pack()
。 For now I changed TextArea.grid()
to TextArea.pack()
to get your code to work. 现在,我将
TextArea.grid()
更改为TextArea.pack()
以使您的代码正常工作。
Next your button command was referencing a function that was after the command. 接下来,您的按钮命令引用了该命令之后的函数。 This does not work outside of a class so you will need to move your
callback()
function above your AnalyzeButton
. 这在类之外不起作用,因此您需要将
callback()
函数AnalyzeButton
。
Next we need to fix the indention on your callback()
function. 接下来,我们需要在您的
callback()
函数上修复缩进。 You must remember indention is very important in python and you should take care to keep your indention clean and consistent. 您必须记住缩进在python中非常重要,并且应注意保持缩进干净一致。
The last thing we needed to fix to get everything working as you were intending is to change: 为了使所有功能正常运行,我们需要修复的最后一件事就是更改:
text_input = Text.get()
To: 至:
text_input = TextArea.get(1.0, END)
You were trying to call get()
on the method that created the text widget and not the widget instance. 您试图在创建文本小部件而不是小部件实例的方法上调用
get()
。
You also need to define from what part of the text widget you want to start reading data and how far you want to read through the text widget. 您还需要定义从文本小部件的哪个部分开始读取数据以及要从文本小部件读取多远。 this is done by applying 2 positions points with
1.0, END
or "1.0", "end-1c"
as tkinter allows for a few ways to apply these points. 这是通过应用带有
1.0, END
或"1.0", "end-1c"
2个位置点来完成的,因为tkinter允许使用几种方法来应用这些点。 This will say 1.0
start at the first line at the first position on that line and END
will say read until the last line of the textbox. 这将表示
1.0
从第一行开始在该行的第一位置,并且END
将说已读,直到文本框的最后一行。
There may be other issues but I only fixed the problems preventing the code from working as intended. 可能还有其他问题,但我只解决了阻止代码按预期工作的问题。 Your code modified to work below:
您的代码已修改为可以在以下工作:
from tkinter import *
root = Tk()
root.title('Title')
TopFrame = Frame(root) # was missing from your code
MiddleFrame = Frame(root)
BottomFrame = Frame(root)
TopFrame.pack(side = TOP)
MiddleFrame.pack()
BottomFrame.pack(side = BOTTOM)
TextArea = Text()
ScrollBar = Scrollbar(root)
ScrollBar.config(command = TextArea.yview)
TextArea.config(height = 25, width = 70,
background = "white", yscrollcommand = ScrollBar.set)
TextArea.pack() # can't use pack and grid on the same window/frame. Changed this to pack()
ScrollBar.pack(side = RIGHT, fill = Y)
padx = 10
pady = 10
TextArea.pack(padx = padx, pady = pady)
# this function needs to be before the command that references it.
def callback():
# fixed indention
text_input = TextArea.get(1.0, END) # you need to reference that widget name not the tkinter method used to create the widget.
print(text_input)
AnalyzeButton = Button(BottomFrame, text = "Analyze", fg = "white", bg = "blue", command = callback)
AnalyzeButton.pack(fill = X, padx = padx, pady = pady)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.