0

我正在使用tkinter基于类的结构,我需要获取文本输入的值。我怎么做?

以下是相关代码:

#GUI
class PseudostreamWidget(tk.Frame):

    def __init__(self, master=None, **kw):
        tk.Frame.__init__(self, master, **kw)
        self.targetIP = tk.Entry(self.Ipconfig)
        self.targetIP.configure(background='black', font='{Lato} 12 {}', foreground='white', highlightbackground='black')
        self.targetIP.configure(highlightcolor='black', justify='left', relief='raised')
        _text_ = '''Friend's Public IP Here'''
        self.targetIP.delete('0', 'end')
        self.targetIP.insert('0', _text_)
        self.targetIP.grid(column='1', row='0')
        #
        #
        #
        target_ip = self.targetIP
        #
        # 

我如何让它target_ip在课外打印?

if __name__ == '__main__':
    root = tk.Tk()  
    widget = PseudostreamWidget(root)
    widget.pack(expand=True, fill='both')
    root.mainloop()

print(target_ip)
4

1 回答 1

3

试试这个:

import tkinter as tk


class PseudostreamWidget(tk.Frame):
    def __init__(self, master=None, callback=None, **kwargs):
        super().__init__(master, **kwargs)
        self.targetIP = tk.Entry(self, fg="white", justify="left",
                                 relief="raised", highlightbackground="black",
                                 highlightcolor="black", bg="black",
                                 insertbackground="white", font=("Lato", 12))
        # self.targetIP.delete(0, "end") # No need for this
        self.targetIP.insert(0, "Friend's Public IP Here")
        self.targetIP.grid(column=1, row=0)
        self.targetIP.bind("<Return>", callback)


def submit_data(event=None):
    print("You typed in:", widget.targetIP.get())

root = tk.Tk()
widget = PseudostreamWidget(root, callback=submit_data)
widget.pack()
root.mainloop()

要使用它,只需在条目内写一些东西,然后按Enter键盘上的键。

它传入一个函数 ( submit_data) 并绑定到它。

于 2021-04-23T16:56:25.750 回答