简体   繁体   English

如何在控制台中打印文本框中的文本?

[英]How Do I have the text in the text box get printed in the console?

OK, So i just started learning how to code in python and I really like making gui with tkinter.好的,所以我刚开始学习如何在 python 中编码,我真的很喜欢用 tkinter 制作 gui。 I have run into a problem i just cannot fix.我遇到了一个我无法解决的问题。 hopefully someone will be able to help.希望有人能提供帮助。

import tkinter as tk
window = tk.Tk()

text_box = tk.Text()


text_box.pack()

window.geometry("700x500+50+60")

window.title('Welcome!')



label = tk.Label(
     text="Hello, Welcome to my first app!",
     foreground="white",
     background="black"
)

button = tk.Button(
        text="click me!",
        width="25",
        height="5"
)




button.pack()
label.pack()



window.mainloop()

so i just need to have whats in the textbox print into console when i press the button.所以当我按下按钮时,我只需要将文本框中的内容打印到控制台中。

Use print(text_box.get(1.0, 'end-1c')) .使用print(text_box.get(1.0, 'end-1c')) The 1.0 means "line 1.character 0". 1.0表示“第 1 行字符 0”。 The 'end-1c' means "end of Text minus 1 character". 'end-1c'表示“ Text结尾减去 1 个字符”。 The entire thing encompasses the range you want to return (inclusively).整个事情包括您想要返回的范围(包括)。 You want to omit the last character because, Text widgets automatically have a newline character in them ~ unless you want that character in your print, then just use 'end' .您想省略最后一个字符,因为Text小部件会自动在其中包含换行符 ~ 除非您希望在打印中使用该字符,否则只需使用'end'

The below example is the OOP implementation of your app.下面的示例是您的应用程序的 OOP 实现。 It is heavily commented to give you direction.大量评论为您提供指导。

import tkinter as tk


#extend root
class App(tk.Tk):
    #application constants
    WIDTH, HEIGHT, X, Y, TITLE = 700, 500, 50, 60, 'Welcome'

    #class constructor
    def __init__(self):
        #super()
        tk.Tk.__init__(self)

        #force this row and column to take up as much space as possible
        #affects textfield and welcome
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        
        #same thing as your "text_box"
        self.textfield = tk.Text(self)
        self.textfield.grid(row=0, column=0, columnspan=2, sticky='nswe')
        
        #same thing as your "label" but given a more descriptive name
        self.welcome = tk.Label(self, text="Welcome to my first app!", fg="white", bg="black")
        self.welcome.grid(row=1, column=0, sticky='nswe')
        
        #same thing as your "button" but given a more descriptive name, and a purpose
        #note: width and height are in characters, not pixels ~ same for Label and Text
        self.print_btn = tk.Button(self, text="print", width=5, height=1, command=self.toConsole)
        self.print_btn.grid(row=1, column=1)
        
    def toConsole(self):
        #how to send text from a textfield to the console
        print(self.textfield.get(1.0, 'end-1c'))
        
        
#properly initialize your app
if __name__ == '__main__':
    app = App()
    app.title(App.TITLE)
    app.geometry(f'{App.WIDTH}x{App.HEIGHT}+{App.X}+{App.Y}')
    app.resizable(width=False, height=False)
    app.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM