Python GUI questions

M

maiden129

Hello,

I'm using python 3.2.3 and I'm making a program that show the of occurrences of the character in the string in Tkinter.

My questions are:

How can I make an empty Entry object that will hold a word that a user will enter?

How to make an empty Entry object that will hold a single character that the user will enter?


How to A Button object with a text equal to "Count"?

Thanks in advance.
 
M

maiden129

This sounds like homework. Have you had a try at it yourself before

asking? If so, show us your code, and point out where the problem is;

if not, give it your best effort before you try to get someone else to

do it for you, as that's the only way to learn!



ChrisA

This is not homework, I'm trying to learn how do I create a empty object. That was my main question.
 
M

maiden129

This sounds like homework. Have you had a try at it yourself before

asking? If so, show us your code, and point out where the problem is;

if not, give it your best effort before you try to get someone else to

do it for you, as that's the only way to learn!



ChrisA

This is not homework, I'm trying to learn how do I create a empty object. That was my main question.
 
R

Rick Johnson

Hello,

I'm using python 3.2.3 and I'm making a program that show
the of occurrences of the character in the string in
Tkinter.

My questions are:

How can I make an empty Entry object that will hold a word
that a user will enter?

I believe you meant to say: "How can i CREATE an entry field to accommodate user input?"

Easy.
How to make an empty Entry object that will hold a single
character that the user will enter?

Not as easy, but still quite doable. Do you want to filter the input, allowing only a single character?
How to A Button object with a text equal to "Count"?

Easy-pee-see. Follow this yellow brick road to enlightenment.

http://effbot.org/tkinterbook/tkinter-whats-tkinter.htm
 
M

maiden129

I believe you meant to say: "How can i CREATE an entry field to accommodate user input?"



Easy.







Not as easy, but still quite doable. Do you want to filter the input, allowing only a single character?






Easy-pee-see. Follow this yellow brick road to enlightenment.



http://effbot.org/tkinterbook/tkinter-whats-tkinter.htm

Hello,

Here is my try to answer some of questions:


from tkinter import *

class word:
def __init__(self,Entry,Character):
window = Tk()
window.title("Widget")

top = Tk()
L1 = Label(top, text="Enter a string")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)

E1.pack(side = RIGHT)

top.mainloop()

L2 = Label(bottom, text="Number of single characters")
L2.pack( side = LEFT)
E2 = Entry(bottom, bd =5)

button = Tkinter.Button(bottom, text ="Count", command = countCharacter).pack()

def countChacater(self):
count = word.count(character)

I'm just struggling with only how to create an object that will hold a single character that the user will enter.
 
J

Jason Swails

allowing only a single character?

Hello,

Here is my try to answer some of questions:


from tkinter import *

class word:
def __init__(self,Entry,Character):
window = Tk()
window.title("Widget")

top = Tk()
L1 = Label(top, text="Enter a string")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)

This is unlikely to work. You have overwritten the Entry widget from
tkinter, meaning that E1 will not be an Entry (unless you pass
tkinter.Entry to a word() instance, which seems redundant).

My suggestion is actually to generate classes derived from tkinter widgets
(I often use Frame, since it's quite generic and can act as a container for
any other widget easily).

E1.pack(side = RIGHT)

top.mainloop()

L2 = Label(bottom, text="Number of single characters")
L2.pack( side = LEFT)
E2 = Entry(bottom, bd =5)

button = Tkinter.Button(bottom, text ="Count", command =
countCharacter).pack()

def countChacater(self):
count = word.count(character)

I'm just struggling with only how to create an object that will hold a
single character that the user will enter.

This is tricky. The approach I would take is to generate an entry widget
and then bind all key-press events in that widget to a method that checks
how long the input string is. If it is longer than a single character,
reject the new letter and optionally raise an alert (using, e.g.,
tkMessageBox.showwarning).

If you want the count on the button to be updated continuously, you'll need
to update that counter every time either the input string or character is
changed.

Good luck,
Jason
 
R

Ranting Rick

Here is my try to answer some of questions:

[snip code]
I don't understand why you are wrapping this code into a class. Are
you trying to create something reuseable?
I'm just struggling with only how to create an object that
will hold a single character that the user will enter.

I would suggest that you scrape this code and start over, and a good
starting point would be at the BEGINNING.

All (well, *most*) GUI applications begin with a "root window". Once
you have created the main window you can start placing widgets inside
the window. This is the basic outline of a Tkinter GUI application.

1. Create the root window.
2. Create all the needed sub-widgets and arrange them properly.
3. Start processing user events.

So using the code you provided (and rearranging it to make sense) you
would end up with this:

## START CODE ##(Python 3.x)
import tkinter as tk
from tkinter.constants import LEFT

def cbButton(self):
print('I should do something here!')

root = tk.Tk()
root.title("Window")
w=tk.Label(root, text="Enter a string")
w.pack(side=LEFT)
e1 = tk.Entry(root, bd=5)
e1.pack(side=LEFT)
b=tk.Button(root, text="Count", command=cbButton)
b.pack(padx=5, pady=5)
root.mainloop()
## END CODE ##

Notice that i used a more intelligent form of import that will
maintain a clean namespace. Tkinter is a very BLOATED module, and
since you are just learning you won't be aware of the names that could
cause problems.

Also notice that my style is consistent. Not following an accepted
coding style is condemnable, however, being inconsistent with your
style is abominable!

PS: Also, trim those excessive quotes please.
 
M

maiden129

On Mar 19, 8:25 pm, maiden129 <[email protected]> wrote:



So should I redo my other code that I created with the radioButtons to change the colors of a text?

from tkinter import *

class buttons:
def __init__(self):
window = Tk()
window.title("Radio buttons and buttons")

self.var = IntVar()


w1 = Radiobutton(window, text="Red", variable=self.var, value=1,command=changeColor).pack()
w2 = Radiobutton(window, text="Yellow", variable=self.var, value=2,command=changeColor).pack()
w3 = Radiobutton(window, text="White", variable=self.var, value=3,command=changeColor).pack()
w4 = Radiobutton(window, text="Grey", variable=self.var, value=4,command=changeColor).pack()
w5 = Radiobutton(window, text="Green", variable=self.var, value=5,command=changeColor).pack()
B1 = Button ( window, text="<=", command=LEFT).pack()
B2 = Button ( window, text="=>", command=RIGHT).pack()

def changeColor(self):
self.var.get()



window.mainloop()


buttons()
 
R

Rick Johnson

So should I redo my other code that I created with
the radioButtons to change the colors of a text?

I believe so. Although you really should explain what your trying to achieve with this code. There is nothing wrong with wrapping some widgets into a single reusable class, however, your examples look more like more "using a hammer to drive screws".

For instance, you have two classes named "button" and "word" (psst: those identifiers should start with a capitol letter BTW!) which are basically two independent Tkinter root windows (even though they don't inherit from Tkinter.Tk).

"Tkinter.Tk" is meant to be used as the first window, from there you can add as many sub-frames, sub-widgets, or even sub-windows (instances of Tkinter.Toplevel) as you want.

If you want more than one window, for your GUI application, then instance as many "Tkinter.Toplevel" windows as you like AFTER you create the ONE AND ONLY Tkinter.Tk window.

If however you want to "group" a number of widgets inside a single window, then use the "Tkinter.Frame" to hold them.
 
T

Terry Reedy

import tkinter as tk
from tkinter.constants import LEFT

def cbButton(self):
print('I should do something here!')

root = tk.Tk()
root.title("Window")
w=tk.Label(root, text="Enter a string")
w.pack(side=LEFT)
e1 = tk.Entry(root, bd=5)
e1.pack(side=LEFT)
b=tk.Button(root, text="Count", command=cbButton)
b.pack(padx=5, pady=5)
root.mainloop()

when I run this, and click the button, I get
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Programs\Python33\lib\tkinter\__init__.py", line 1442, in
__call__
return self.func(*args)
TypeError: cbButton() missing 1 required positional argument: 'self'

when I remove 'self' from cbButton, I get the expected
I should do something here!

When I enter something in the Entry box and hit return, nothing happens.
I presume that is because the example is not complete.
 
R

Rick Johnson

[snip code]

when I run this, and click the button, I get:

TypeError: cbButton() missing 1 required positional argument: 'self'

...when I remove 'self' from cbButton, I get the expected
I should do something here!

Oops :), good catch Terry! I had copy-pasted the OP's original method however i forgot to remove the "self" parameter; then i had to convert my Python2.x code into 3.x code, which was not terribly difficult, however, this "pit stop" was distracting enough that I forgot to click the button while testing.
When I enter something in the Entry box and hit return,
nothing happens.

My example does not bind the "KeyPress-ReturnKey" to any callback, so what were you expecting to happen when you pressed enter?
I presume that is because the example is not complete.

Well it's as complete as it needs to be :). Any more complete and it wouldless of a teaching exercise and more of a "copy-paste" exercise. I did notwant to just gift wrap an answer for the OP. My hope is that he has the basic skills to turn that incomplete example into useable code. If not, well,then he needs to study that tutorial link i sent early in the thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top