简体   繁体   English

Python控制台输出到变量

[英]Python console output to variable

I have seen a few posts on this site and some others that cover a similar topic don't quite seem to reach the result i am looking for with python v3. 我在该网站上看到了一些帖子,而涉及类似主题的其他帖子似乎并没有达到我在python v3中寻找的结果。 My aim is to have a popup window containing two entry boxes for a username and a password which i can then output as variables named username and password, to then in turn use to login to a website which i already have scripted. 我的目标是要有一个弹出窗口,其中包含两个用于输入用户名和密码的输入框,然后我可以将其输出为名为用户名和密码的变量,然后依次用于登录到我已经编写脚本的网站。 The code i have so far is: 我到目前为止的代码是:

from tkinter import *

def show_entry_fields():
   print("Username: %s\nPassword: %s" % (e1.get(), e2.get()))

master = Tk()
Label(master, text="Username").grid(row=0)
Label(master, text="Password").grid(row=1)

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Submit', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

I am getting stuck with working out how to take the output that shows in the console after pressing submit and getting these two lines to become the variables? 在按提交并让这两行成为变量之后,如何解决在控制台中显示的输出问题,我将陷入困境。 Any help or suggestions would be greatly appreciated. 任何帮助或建议,将不胜感激。 Thanks in advance, 提前致谢,

James 詹姆士

You can use tkinter's variables: 您可以使用tkinter的变量:

username = StringVar()
password = StringVar()

And then when you define the entrys, add argument textvariable : 然后在定义textvariable ,添加参数textvariable

e1 = Entry(master, textvariable = username)
e2 = Entry(master, textvariable = password)

To get the value from this variable, call .get() function on it. 要从此变量获取值,请在其上调用.get()函数。

I suppose your question is "How do I get access on a self destructing window?" 我想您的问题是“如何在自毁窗口上访问?” - You don't. -你不知道

First Approach 第一种方法

You access it before it destroys itself. 您可以破坏之前对其进行访问。

How is that? 那个怎么样? - You bind and event from the child (pop-up) to the parent and as soon as your submit button is fired, you generate the event as a notification for the parent like "I am soon to be destroyed" (see this question answered by Bryan Oakley for more info ). -绑定事件并将事件从子项(弹出窗口)绑定到父项,并在触发提交按钮后立即生成事件,作为对父项的通知,例如“我即将被销毁”(请参阅​​已回答的问题)由Bryan Oakley提供,以获取更多信息 )。

One very important thing - the window must not destruct itself as long as it should return something. 一件非常重要的事情-窗口只要能返回某些东西就不能破坏自己。 The parent kills it after retrieving the data from it. 父级从中检索数据后将其杀死。

Second Approach 第二种方法

You use eg tkinter.StringVar for storing your data. 您使用例如tkinter.StringVar来存储数据。

But you do not have them inside your popup but inside your parent widget, so they survive even the Entry widgets are destroyed. 但是您没有将它们放在弹出窗口中,而是放在父窗口小部件中,因此即使Entry小部件被销毁,它们也能幸免。

For this approach, use the Information from Stevo but pass them on to the pop up during construction time as parameters stored inside your parent. 对于这种方法,请使用来自Stevo的信息,但在构建期间将其作为存储在父级内部的参数传递给弹出窗口。

You also need to use lambda to send the arguments to the subroutine. 您还需要使用lambda将参数发送到子例程。 Try this: 尝试这个:

from tkinter import *

def show_entry_fields(v1, v2):
   print("Username: %s\nPassword: %s" % (v1.get(), v2.get()))


if __name__ == "__main__":
  master = Tk()
  Label(master, text="Username").grid(row=0)
  Label(master, text="Password").grid(row=1)

  v1 = StringVar()
  v2 = StringVar()

  e1 = Entry(master, textvariable=v1)
  e2 = Entry(master, textvariable=v2)

  e1.grid(row=0, column=1)
  e2.grid(row=1, column=1)

  Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
  Button(master, text='Submit', command=(lambda : show_entry_fields(v1, v2))).grid(row=3, column=1, sticky=W, pady=4)

  master.mainloop()

UPDATE: I guess I should do as I preach, so I added a local context to the main part. 更新:我想我应该在宣讲时做,所以我在主体部分添加了本地上下文。

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

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