Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-24-2008, 06:42 AM
|
#1
|
|
Member
Registered: Oct 2006
Distribution: Debian x64
Posts: 198
Rep:
|
basic python entry tkinter widget how-to
I'm trying to use entry to get a response:
Code:
from Tkinter import *
root = Tk()
master = Tk()
parent = Tk()
e = Entry(master)
e.pack()
e.focus_set()
def callback():
answer = e.get()
print answer
return answer
b = Button(master, text="get", width=10, command=callback)
b.pack()
generates the widget but I would like to store the response as a variable (possibly keep track of responses eventually). This isn't working out so far.
1. What do I need to do?
2. When I do get the answer (printed, not as a variable), it comes with a new line, how do I eliminate the new line, or get it to signal enter?
|
|
|
|
03-24-2008, 03:56 PM
|
#2
|
|
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142
Rep:
|
DISCLAIMER: I'm still putzing around with Python, so don't take my answers as gospel.
You could do something like this...
Code:
from Tkinter import *
root = Tk()
master = Frame(root)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
answer = ""
def setanswer():
global answer
answer = e.get()
def printanswer():
global answer
print answer
b1 = Button(master, text="SET", width=10, command=setanswer)
b1.pack()
b2 = Button(master, text="PRINT", width=10, command=printanswer)
b2.pack()
root.mainloop()
I set the answer variable as a blank string, then in the functions, I called the global answer variable. It jumps the namespace barrier to get the higher-up variable.
In my modification, the setanswer function sets the variable to whatever is typed into the entry box. The printanswer function prints what is in the answer variable. You can type something new into the entry box, but until you click SET, the variable doesn't change.
I also set master to a frame, and got rid of parent. You were creating three root windows with the three "= Tk()" lines.
Python normal ends a print command with a newline, unless you append a comma at the end... You can also format your print line, something like...
Code:
print 'The answer is "%s".' % answer
|
|
|
|
03-30-2008, 10:36 AM
|
#3
|
|
Member
Registered: Oct 2006
Distribution: Debian x64
Posts: 198
Original Poster
Rep:
|
Well, I've been piddling around with this program for a while. I got the program down a while ago (Make a guess, check it, I even got it printing to tkinter), but I can't get it to enter with tkinter.
Thoughts?
Code:
#! /usr/bin/env python
import cinco
import gtk
import Tkinter
from Tkinter import *
correct = cinco.a
correct2 = cinco.word
correct3 = correct2.lower()
correct4 = list(correct3)
reply = []
score = 0
answer = ""
fixed = list(answer)
root = Tk()
master = Frame(root)
master.pack()
e = Entry(master)
e.pack()
e.focus_set()
w = Label(root, text="Lingo Cinco \n A Guessing Game with Five letters and no repeats")
w.pack()
w = Label(root, text="your guess | points")
w.pack()
def setanswer():
global answer
answer = e.get()
def printanswer():
global answer
print answer
b1 = Button(master, text="SET", width=10, command=setanswer)
b1.pack()
b2 = Button(master, text="PRINT", width=10, command=printanswer)
b2.pack()
def IsInt( str ):
try:
num = int(str)
return 1
except ValueError:
return 0
def checkscore( str ):
check = list(str)
s1 = set(correct4)
s2 = set(check)
s3 = set.intersection(s1 , s2)
s4 = len(s3)
s5 = 0
s6 = 0
s7 = 0
s8 = 0
s9 = 0
s10 = 0
int(s4)
if check[0] == correct4[0]:
s6 = 10
if check[1] == correct4[1]:
s7 = 10
if check[2] == correct4[2]:
s8 = 10
if check[3] == correct4[3]:
s9 = 10
if check[4] == correct4[4]:
s10 = 10
return s4 + s5 + s6 + s7 + s8 + s9 + s10
setanswer()
printanswer()
while fixed != correct4:
if len(answer) != 5 and answer == 'stop':
w = Label(root, text="Good Bye!")
w.pack()
break
elif len(answer) != 5 and answer == 'quit':
w = Label(root, text="Good Bye!")
w.pack()
break
elif len(answer) != 5 and answer == 'exit':
w = Label(root, text="Good Bye!")
w.pack()
print "Good Bye!"
break
elif len(answer) != 5:
w = Label(root, text="Your answer must be five letters long with no duplicates!")
w.pack()
elif len(answer) == 5:
if IsInt(reply) == 1:
w = Label(root, text="Why would you type a number?")
w.pack()
print "Why would you type a number?"
else:
fixed = list(answer)
newscore = checkscore(fixed)
print fixed, correct4, newscore
george = [answer, newscore]
w = Label(root, text= george)
w.pack()
#w = Label(root, text= correct4)
#w.pack()
#w = Label(root, text= newscore)
#w.pack()
else:
print "Your answer must be five letters long with no duplicates!"
while fixed == correct4:
print "good job!"
root.mainloop()
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:11 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|