how to use tkinter widget in django

2,584 views
Skip to first unread message

jimgardener

unread,
Oct 26, 2010, 7:17:44 AM10/26/10
to Django users
is it possible to use the sliderlike Scale widget from tkinter in
django?I am coding a web app where I need to get a numerical value
entered by the user.I thought ,instead of asking the user to enter a
value in a textfield ,I would provide a slider which he can move
between the minimum and maximum values.
Originally I designed the model and form like this

class MyModel(django.models.Model):
user_enrty=models.IntegerField()

class MyModelForm(django.forms.ModelForm):
class Meta:
model=MyModel

In tkinter ,I can create a scale widget like
master = Tk()
w = Scale(master, from_=0, to=100)
w.pack()
and get the current position using w.get()

How can I use this in django?Can I directly put the tkinter widget in
my model like ,
class MyModel(django.models.Model):
user_enrty=tkinter.Scale(...)

Any help would be greatly appreciated
thanks
jim




Steve Holden

unread,
Oct 26, 2010, 8:07:28 AM10/26/10
to django...@googlegroups.com
Unfortunately you cannot do this. Tkinter assumes direct control of the
desktop, whereas Django is a web system that must use HTML (or similar)
to deliver markup to the user's desktop using HTTP. Tkinter just isn't a
web technology.

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

Nelson Saraiva

unread,
Oct 26, 2010, 8:18:15 AM10/26/10
to django...@googlegroups.com
hi,
do this with JavaScript.





2010/10/26 jimgardener <jimga...@gmail.com>




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Nelson Saraiva.
-- -- -- -- -- -- -- --
""" Men are from Mars. Women are from Venus. Computers are from hell. "”"

jimgardener

unread,
Oct 26, 2010, 8:50:07 AM10/26/10
to Django users
thanks for the replies..
I am wondering if javascript is the only alternative if I want to use
such a custom widget.
If anyone knows about any such python widget please tell me..
regards
jim

Jirka Vejrazka

unread,
Oct 26, 2010, 8:58:06 AM10/26/10
to django...@googlegroups.com

You might want to dig a bit into website design to understand how
JavaScript-based systems work. That would help you to understand why
you *cannot* use any Python widget for such task. (polite hint -
JavaScript runs in the browser and can be started from a HTML page
served by a server)

Cheers

Jirka

Steve Holden

unread,
Oct 26, 2010, 9:00:28 AM10/26/10
to django...@googlegroups.com
Jim:

The reason you can forget using Python is that Python isn't available on
the browser. You have to deliver content from Django that the user's
browser can execute, and that isn't likely to include Python in most cases.

http://www.learnthenet.com/learn-about/how-the-web-works/

ringemup

unread,
Oct 26, 2010, 10:11:18 AM10/26/10
to Django users
Try the HTML5 <input type="range">. On some current browsers, it'll
just appear as a text field, although you can supplement it with
javascript. Moving forwards, as more browsers support HTML5 forms,
it'll appear as a slider widget. See [1].

[1]http://diveintohtml5.org/forms.html#type-range

jimgardener

unread,
Oct 26, 2010, 2:47:38 PM10/26/10
to Django users
hi
thanks for the link.
I tried this,In firefox3.6.11 ,it is rendered as a text field
In chrome it shows a slider.

...
<p>
<label for="id_user_entry">select numerical value using slider:</
label>
<input type="range"
min="0"
max="1000"
step="2"
value="6" id="id_user_entry"/>
</p>
<p>
{{myform.user_entry.errors}}
</p>
...
However,I am getting validation error upon processing the form
I put the field as an IntegerField in my model .I am not sure if that
is ok.

class MyModel(django.models.Model):
user_enrty=models.IntegerField()

class MyModelForm(django.forms.ModelForm):
class Meta:
model=MyModel

In the view ,I checked for validity of form
...
form_data=get_form_data(request)
form=MyModelForm(form_data)
if request.method=='POST':
print 'request.POST=',request.POST
form_is_valid=form.is_valid()
print 'form is valid:',form_is_valid
if form_is_valid:
newentry=form.save()
print 'newentry=',newentry
return redirect('home')
else:
print 'form is not valid'
...

Chrome complains that the slider field is required .The request.POST
is printed without the 'user_entry' key
I tried the same with firefox(which rendered the slider as textbox)
There also I get 'field is required error'

Any idea how I can correct this? Why do the data in input
type="range" not coming in the request.POST querydict?

thanks
jim

elijah rutschman

unread,
Oct 26, 2010, 2:54:13 PM10/26/10
to django...@googlegroups.com
>  <input type="range"
>       min="0"
>       max="1000"
>       step="2"
>       value="6" id="id_user_entry"/>

Try adding a name="user_entry" attribute to the input.

jimgardener

unread,
Oct 26, 2010, 3:12:41 PM10/26/10
to Django users
thanks elijah..that worked

how come id="id_user_entry" not enough?
regards
jim
Reply all
Reply to author
Forward
0 new messages