简体   繁体   English

我想将输入文本从文本框打印到控制台。 下面是我的代码

[英]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.

相关问题 打印到控制台。 现在我想打印到 CSV 文件 - Prints to console. Now I want to print to CSV file 我的代码通过PyCharm中的main.py运行,而不是通过控制台运行。 为什么会这样呢? - My code runs through main.py in PyCharm but not from console. Why is this so? 我想通过 API 数据 go 并在我的控制台中使用打印显示它 - I want to go through an API data and display it with print in my console 我想使用 python 从数字之间删除加法符号,请在下面找到我的代码 - I want to Remove the addition symbol from in-between the numbers using python and please find the below my code 我的代码没有打印我想要它打印的内容 - My code doesn't print what I want it to print 我想从下面的一堆列表中获取纯文本 - I want to get the pure text from a bunch of lists below 我想将下面的代码(神经网络)从keras转换为pytorch - i want to convert the code below(neural network) from keras to pytorch 我希望我的代码仅在结果与之前的结果不同且不再相同时才打印 - I want my code to print only if the result is different from previous result and not the same thing again 如何在下面的代码中打印字典中的值? - How can I print a value from dictionary on the below code? 如果我只想在条目下方添加一个简单的注释框,我应该使用Django的注释框架还是编写自己的注释框架? - If I just want a simple comment box below my entry, should I use Django's comment framework or write my own?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM