[英]Why won't my program read input from entry?
from Tkinter import *
import requests
import base64
var = "lol"
var2 = "lol"
var3 = "111111"
var4 = "31039888252154&31039888252154&0&X168&1411869345&IQSAXYOS&"+var2+"&"+var3+"&&1&3&1&ubugg@zetmail.com&goohoo&1411603529&&2&"
encoded = base64.b64encode(var4)
var5 = encoded
payload = {'X1': '218',
'X127': 'X325',
'X171':var,
'X677':'1420103258',
'X678':'208180484',
'X691':var5,
'X763':'1'}
def bluh(e):
var = e1.get()
var2 = e2.get()
var3 = e3.get()
def Send(e):
requests.post("http://us23.chatzy.com/", data=payload)
def p1(e):
print var
print var2
print var3
#Sh!t in the GUI
root = Tk()
root.title("Spam Project")
root.geometry("200x200")
app = Frame(root)
app.grid()
button1 = Button(app, text = "Send")
button1.grid()
button1.bind('<Button-1>', Send)
e1 = Entry(root)
e1.grid()
e1.insert(0,"Message")
e2 = Entry(root)
e2.grid()
e2.insert(0,"Name")
e3 = Entry(root)
e3.grid()
e3.insert(0,"Color in Hex")
button2 = Button(app, text = "Submit")
button2.grid()
button2.bind('<Button-1>', bluh)
button3 = Button(app, text = "Print")
button3.grid()
button3.bind('<Button-1>', p1)
var = e1.get()
var2 = e2.get()
var3 = e3.get()
#Event loop
root.mainloop()
Basically, what this is supposed to do is take a name, message and hex color and send them as an HTTP request to a chat, this all works in terminal but then I tried to stitch a GUI on top, at the very top, var
, var2
, var3
are all default values and I have the submit button to change those values, but it simply does not work, so I made the "print" button to print the three values and they do not change, can anyone tell me what's wrong with my code and how can I make the three entries change the variables according to what's inside them? 基本上,这应该是使用名称,消息和十六进制颜色并将其作为HTTP请求发送到聊天,这些都可以在终端中使用,但是后来我尝试在最上面的
var
上拼接一个GUI , var2
, var3
都是默认值,并且我有“提交”按钮来更改这些值,但是它根本行不通,所以我制作了“打印”按钮来打印这三个值,并且它们没有改变,谁能告诉我这是什么我的代码有问题,如何使这三个条目根据其中的内容更改变量?
Here's what the variables do. 这是变量的作用。
The usual way in GUI code to avoid using globals is to use a class. GUI代码中避免使用全局变量的通常方法是使用类。 I hope this isn't too advanced for you.
我希望这对您来说不太高级。 But if it is, well you gotta learn this stuff sometime if you want to do serious programming.
但是如果是这样,那么,如果您想进行认真的编程,就必须在某个时候学习这些知识。 :) So read about classes in the Python docs, and then if you still can't figure out what something in my code's doing, ask & I'll try to explain.
:)因此,请阅读Python文档中的类,然后,如果您仍然无法弄清我的代码在做什么,请询问&我将尝试解释。
I've commented out the actual requests
stuff for testing purposes. 我已经出于测试目的而注释掉了实际的
requests
内容。
#! /usr/bin/env python
from Tkinter import *
#import requests
import base64
def build_payload(var1, var2, var3):
var4 = "31039888252154&31039888252154&0&X168&1411869345&IQSAXYOS&" \
+ var2 + "&" + var3 \
+ "&&1&3&1&ubugg@zetmail.com&goohoo&1411603529&&2&"
print 'var4 = [%s]\n' % var4
var5 = base64.b64encode(var4)
payload = {
'X1': '218',
'X127': 'X325',
'X171': var1,
'X677': '1420103258',
'X678': '208180484',
'X691': var5,
'X763': '1'
}
return payload
class my_GUI(object):
def __init__(self):
#Build GUI
root = Tk()
root.title("Spam Project")
root.geometry("200x200")
app = Frame(root)
app.grid()
button = Button(app, text = "Send")
button.grid()
button.bind('<Button-1>', self.send_data)
self.e1 = Entry(root)
self.e1.grid()
self.e1.insert(0, "Message")
self.e2 = Entry(root)
self.e2.grid()
self.e2.insert(0, "Name")
self.e3 = Entry(root)
self.e3.grid()
self.e3.insert(0, "Color in Hex")
button = Button(app, text = "Print")
button.grid()
button.bind('<Button-1>', self.print_data)
#Start tkinter event loop
root.mainloop()
def print_data(self, e):
var1 = self.e1.get()
var2 = self.e2.get()
var3 = self.e3.get()
print var1, var2, var3
def send_data(self,e):
var1 = self.e1.get()
var2 = self.e2.get()
var3 = self.e3.get()
print var1, var2, var3
payload = build_payload(var1, var2, var3)
print 'Sending payload', payload
#requests.post("http://us23.chatzy.com/", data=payload)
def main():
my_GUI()
if __name__ == '__main__':
main()
PS. PS。 I've changed the names of some of the functions from what they were in your code, but I've left the variables names (mostly) as they were.
我已经从代码中更改了一些函数的名称,但我保留了变量名(大部分是)。 In future, try to use more meaningful names - it makes life easier both for yourself & for other people who are trying to read your code.
将来,请尝试使用更有意义的名称-这样会使您自己以及其他尝试读取您的代码的人的生活更加轻松。
To use global variables in Python you need the global
statement. 要在Python中使用全局变量,您需要使用
global
语句。 So, you need to modify your bluh
function like this: 因此,您需要像这样修改
bluh
函数:
def bluh(e):
global var, var2, var3
var = e1.get()
var2 = e2.get()
var3 = e3.get()
(I've also fixed the code to use e1
, e2
, e3
resp. instead of e1
only) (我也固定在代码中使用
e1
, e2
, e3
RESP。代替e1
只)
In your version you were creating new, local variables var
, var2
and var3
inside the bluh
function, having nothing to do with the global ones defined at the top of your file. 在您的版本中,您正在
bluh
函数内创建新的局部变量var
, var2
和var3
,与文件顶部定义的全局变量无关。 Please also note that in general using global variables is discouraged. 另请注意,一般不建议使用全局变量。
The same goes to the p1
function, as you also want to access globals. p1
函数也是如此,因为您还想访问全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.