How do you do this in python with tk?

A

Ali

I have a the list:

info = [['Ali',18],
['Zainab',16],
['Khalid',18]]

I want this info to show on a tk window like this:

|--|------------------------|
|TK|Blah |
|--|------------------------|
|Name: Ali |
|Age: 18 |
| |
|Name: Zainab |
|Age: 16 |
| |
|Name: Khalid |
|Age: 18 |
|---------------------------|

I was wondering how to do this?
 
J

Jeremy Bowers

I have a the list:

info = [['Ali',18],
['Zainab',16],
['Khalid',18]]

I want this info to show on a tk window like this:

|--|------------------------|
|TK|Blah |
|--|------------------------|
|Name: Ali |
|Age: 18 |
| |
|Name: Zainab |
|Age: 16 |
| |
|Name: Khalid |
|Age: 18 |
|---------------------------|

I was wondering how to do this?

http://www.pythonware.com/library/tkinter/introduction/grid.htm

Do you even try to figure these things out before posting?
 
J

Jeff Epler

You could create a Text widget and insert lines of text in it.

import Tkinter
def add_rows(w, titles, rows):
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

If there are many lines, you can use a scrollbar widget to scroll the
text widget. You can use the tabs= argument of the Text widget to
adjust the alignment of the second column. If you want to make the
items "interactive", you can use tags and tag_bind to react to things
like button presses. You can set the text widget to be "disabled" to
keep the user from changing the contents.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBZdTNJd01MZaTXX0RAuZqAJ9hsp8Z9V/SN+uP8NEqofZlDD6O4gCdEdgt
caOM+FJ+imW7c2vSj8Bcg7I=
=nWpO
-----END PGP SIGNATURE-----
 
A

Ali

Jeremy Bowers said:
I have a the list:

info = [['Ali',18],
['Zainab',16],
['Khalid',18]]

I want this info to show on a tk window like this:

|--|------------------------|
|TK|Blah |
|--|------------------------|
|Name: Ali |
|Age: 18 |
| |
|Name: Zainab |
|Age: 16 |
| |
|Name: Khalid |
|Age: 18 |
|---------------------------|

I was wondering how to do this?

http://www.pythonware.com/library/tkinter/introduction/grid.htm

Do you even try to figure these things out before posting?

Yes.
 
A

Ali

Jeff Epler said:
You could create a Text widget and insert lines of text in it.

import Tkinter
def add_rows(w, titles, rows):
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

If there are many lines, you can use a scrollbar widget to scroll the
text widget. You can use the tabs= argument of the Text widget to
adjust the alignment of the second column. If you want to make the
items "interactive", you can use tags and tag_bind to react to things
like button presses. You can set the text widget to be "disabled" to
keep the user from changing the contents.

Jeff

--

I will try out your code. Thank you for helping.
 
A

Ali

You can set the text widget to be "disabled" to
How? I tryed putting diabled in there and disabled=yes and
disabled='yes'. None of them worked :(
 
E

Eric Brunel

Ali said:
I tryed that and it stoped showing the text I want it to show :(

Setting state='disabled' does not only prevent the user from editing the text,
but also prevents *you* from modifying the text via the insert or delete
methods. So whenever you want to insert or delete lines in the text, you must
configure its state to 'normal' before, do the modification, then set back its
state to 'disabled'

HTH
 
A

Ali

Setting state='disabled' does not only prevent the user from editing the text,
but also prevents *you* from modifying the text via the insert or delete
methods. So whenever you want to insert or delete lines in the text, you must
configure its state to 'normal' before, do the modification, then set back its
state to 'disabled'

HTH

OK so I tryed:

import Tkinter
def add_rows(w, titles, rows):
t.state = 'normal'
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

it still wont show the text I want it to.
 
M

Miki Tebeka

Hello Ali,
import Tkinter
def add_rows(w, titles, rows):
t.state = 'normal' w.state = 'normal'
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

HTH.
 
E

Eric Brunel

Ali said:
OK so I tryed:

import Tkinter
def add_rows(w, titles, rows):
t.state = 'normal'

tk options are not exposed as widget attributes, but via the configure method or
dictionary-style indexing. So this should be:

w.configure(state='normal')

or:

w['state'] = 'normal'

What you did only creates a new attribute named "state" for the widget, which
has no meaning at all at tk level.

[snip]

HTH
 
A

Ali

Eric Brunel said:
Ali said:
OK so I tryed:

import Tkinter
def add_rows(w, titles, rows):
t.state = 'normal'

tk options are not exposed as widget attributes, but via the configure method or
dictionary-style indexing. So this should be:

w.configure(state='normal')

or:

w['state'] = 'normal'

What you did only creates a new attribute named "state" for the widget, which
has no meaning at all at tk level.

[snip]

HTH

well I tryed this:

import Tkinter
def add_rows(w, titles, rows):
t.configure(state = 'normal')
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

Well... it shows the window, it doesnt show text, it still lets me type in stuff :(
What is wrong now?
 
E

Eric Brunel

Ali wrote:
[snip]
well I tryed this:

import Tkinter
def add_rows(w, titles, rows):
t.configure(state = 'normal')

REMOVE THAT LINE! Since you use t as a local variable afterwards, this will make
the whole script fail. BTW, this means this isn't the code you actually used,
since it doesn't display any window at all.
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

Well... it shows the window, it doesnt show text, it still lets me type in stuff :(
What is wrong now?

You never set the text state to disabled, so no wonder you can still type text in...
 
A

Ali

Eric Brunel said:
Ali wrote:
[snip]
well I tryed this:

import Tkinter
def add_rows(w, titles, rows):
t.configure(state = 'normal')

REMOVE THAT LINE! Since you use t as a local variable afterwards, this will make
the whole script fail. BTW, this means this isn't the code you actually used,
since it doesn't display any window at all.
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app)
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

Well... it shows the window, it doesnt show text, it still lets me type in stuff :(
What is wrong now?

You never set the text state to disabled, so no wonder you can still type text in...

ok so I typed:

import Tkinter
def add_rows(w, titles, rows):
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app, state='disabled')
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

This now shows the text but, will still let me edit the text in side! :(
 
F

Fredrik Lundh

Ali said:
You never set the text state to disabled, so no wonder you can still type text in...

ok so I typed:

import Tkinter
def add_rows(w, titles, rows):
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")

app = Tkinter.Tk()
t = Tkinter.Text(app, state='disabled')
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

This now shows the text but, will still let me edit the text in side! :(

when the state is set to normal, you can insert text into the widget, and
so can the user. when the state is set to disabled, you cannot insert text,
and neither can the user.

now try setting the state to disabled *after* you've inserted the text.

</F>
 
A

Ali

Thank you all!! it now works just fine! The following is the final code!

import Tkinter
def add_rows(w, titles, rows):
w.configure(state = 'normal')
for r in rows:
for t, v in zip(titles, r):
w.insert("end", "%s:\t%s\n" % (t, v))
w.insert("end", "\n")
w.configure(state='disabled')

app = Tkinter.Tk()
t = Tkinter.Text(app, state='disabled')
t.pack()
info = [['Ali',18],
['Zainab',16],
['Khalid',18]]
add_rows(t, ["Name", "Age"], info)
app.mainloop()

Thank you all again! :):):)
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top