[Tutor PyCZ] přenos hodnoty proměnné z elementu GUI (Tkinter) do hlavního programu
Roman Miklos
RMiklos na pss.sk
Pondělí Říjen 9 16:44:59 CEST 2006
V Tkinteri sa hodnoty premennych ziskavaju vzdy cez get().
Okrem toho by som ti ale doporucil pouzivat objektovo orientovany pristup:
Je to prirodzeny pristup pri Tkinter, usetris si tym namahu a umozni ti
vyriesit rozne uskalia, ktore Tkinter ma.
Okrem toho maju jednotlive GUI-prvky metody, ktore si treba predefinovat,
Napr.v tkSimpleDialog.Dialog treba predefinovat metody body() a apply().
Na to treba vytvorit vlastnu triedu, ktora dedi z tkSimpleDialog.Dialog a
v nej nadefinovat metody.
Toto by bez OO neslo - alebo neviem jak :-)
Tu je priklad, ktory som na ilustraciu vytrhol z jedneho programu
--------------------------------------------------------------------------------------------
# -*- coding: cp1250 -*-
from Tkinter import *
import tkSimpleDialog
class MyConnectionDialog(tkSimpleDialog.Dialog):
''' Connection settings entry dialog '''
def __init__(self, parent, machine = '', userid = '', passwd = ''):
# Show previous values in dialog:
# 1. create object attributes from parameters
self.machine = machine
self.userid = userid
self.passwd = passwd
# 2. call parrent __init__(), which calls the function body()
tkSimpleDialog.Dialog.__init__(self, parent, title = None)
def body(self, master):
self.title(u'Pripojenie na AS/400')
Label(master, text=u"AS/400:").grid(row=0)
Label(master, text=u"Užívateľ:").grid(row=1)
Label(master, text=u"Heslo:").grid(row=2)
self.e1 = Entry(master)
self.e1.insert('0',self.machine)
self.e2 = Entry(master)
self.e2.insert('0',self.userid)
self.e3 = Entry(master, show="*")
self.e3.insert('0',self.passwd)
self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
self.e3.grid(row=2, column=1)
# Set initial focus
if self.machine == '':
return self.e1 # initial focus
elif self.userid == '':
return self.e2 # initial focus
else:
return self.e3 # initial focus
def apply(self):
self.machine = self.e1.get()
self.userid = self.e2.get()
self.passwd = self.e3.get()
self.result = 1
# --- Main Windows GUI Class
class MyGUI:
def __init__(self):
self.root = Tk()
self.root.title(u'Nadpis hlavného okna')
# initial login values
self.csebk = 'IBPPROD'
self.userid= ''
self.pwd = ''
self.connected = False
def nastavenia(self):
if (not self.connected):
settings = MyConnectionDialog(self.root, self.csebk, self.userid,
self.pwd)
if settings.result <> None:
self.csebk = settings.machine
self.userid= settings.userid
self.pwd = settings.passwd
def run(self):
self.nastavenia()
print "Bolo zadane: %s %s %s" % (self.csebk, self.userid, self.pwd)
if __name__ == "__main__":
# Run program
mg = MyGUI()
mg.run()
--------------------------------------------------------------------------------------------
"Jan Šimůnek" <jsim na med.muni.cz>
Sent by: tutor-bounces na py.cz
09.10.2006 15:59
Please respond to
Konference Tutor portalu PyCZ <tutor na py.cz>
To
tutor na py.cz
cc
Subject
[Tutor PyCZ] přenos hodnoty proměnné z elementu GUI (Tkinter) do hlavního
programu
Děkuji, pomohlo to
Jan Šimůnek
_______________________________________________
Tutor mailing list
Tutor na py.cz
http://www.py.cz/mailman/listinfo/tutor
Mgr. Ing. Roman MIKLÓŠ
Prvá stavebná sporiteľňa a.s.
Bajkalská 30, P. O. Box 48
829 48 Bratislava 25
Tel.: +421/ 2 / 582 31 174
Fax: +421/ 2 / 582 31 109
------------- další část ---------------
HTML příloha byla odstraněna...
URL: http://www.py.cz/pipermail/tutor/attachments/20061009/f5019243/attachment.htm
------------- další část ---------------
Netextová příloha byla odstraněna...
Jméno: pokus2.py
Typ: application/octet-stream
Velikost: 2130 bytes
Popis: [žádný popis není k dispozici]
Url : http://www.py.cz/pipermail/tutor/attachments/20061009/f5019243/attachment.obj
Další informace o konferenci Tutor