IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

Problème avec tkSimpleDialog.Dialog


Sujet :

Tkinter Python

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Novembre 2004
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Novembre 2004
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Problème avec tkSimpleDialog.Dialog
    Bonjour,

    Pour une application sur laquelle je travaille j'ai eu besoin d'un widget pour pouvoir choisir une date dans une calendrier.

    J'utilise le framework Tkinter mais il ne semble pas y avoir de widget pre-existant pour ce que je cherche.

    J'ai trouvé une implémentation d'un calendrier qui me plait bien sur http://www.python-forum.de/topic-1835.html. Je l'ai transformer un peu pour l'adapter à mes besoins mais je tombe maintenant sur une autre problème.

    Il s'agit surement d'un bug mineur mais je n'arrive pas à mettre la main dessus.

    J'ai créer une dialog custom héritant de tkSimpleDialog.Dialog. J'y ai placé mes calendrier modifiés. Jusque là, tout va bien.

    Le problème est le suivant: régulièrement lorsque je rempli les 3 champs de la dialog, je n'arrive plus a clicker sur le bouton 'ok'. Pourtant l'utilisation de la touche ENTER ou du focus (sélectionner le bouton avec tab puis faire espace) fonctionne sans problème.

    Il me semble que le handler associée au bouton n'est même pas exécuté.

    Si vous avez une idée je suis prenneur.

    Merci d'avance !

    Folken

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
     
    #!/usr/bin/env python
    # http://www.python-forum.de/topic-1835.html
    '''
        Description:    a calendar-widget for Tkinter
        Version:        0.2
        Copyright:      2004 by Markus Weihs (ehm_dot_weihs_at_utanet_dot_at)
        Created:        2004-07-17
        Last modified:  2004-07-18
        Licence:        free
        Requirements:   Python,Tkinter
    '''
     
    import Tkinter as tk
    import tkSimpleDialog
    import string
    from time import localtime
    from calendar import setfirstweekday, monthcalendar
     
    class TkCalendar(tk.Frame):
     
        def __init__(self,master,y,m,c):
            tk.Frame.__init__(self,master,bd=2,relief=tk.GROOVE)
            self.year = y
            self.month = m
            self.callback = c
            self.date = tk.StringVar()
     
            self.dayArray = ["Su","Mo","Tu","We","Th","Fr","Sa"]
            self.col = 0
            setfirstweekday(6)
     
            f = tk.Frame(self)
            f.pack(fill=tk.X)
            tk.Button(f,
                text='<<',
                command=self.__decreaseYear,
                relief=tk.FLAT).pack(side=tk.LEFT)
            tk.Button(f,
                text='<',
                command=self.__decrease,
                relief=tk.FLAT).pack(side=tk.LEFT)
            self.l = tk.Label(f,text="%.2i.%i" % (self.month,self.year))
            self.l.pack(side=tk.LEFT,padx=12, expand=True)
            tk.Button(f,
                text='>>',
                command=self.__increaseYear,
                relief=tk.FLAT).pack(side=tk.RIGHT)
            tk.Button(f,
                text='>',
                command=self.__increase,
                relief=tk.FLAT).pack(side=tk.RIGHT)
     
            self.c = tk.Canvas(self,
                width=140,height=135,
                bg='white',bd=2,relief=tk.GROOVE)
            self.c.bind('<1>', self.__click)
            #self.c.bind('<3>', self.__clickRight)
            self.c.pack()
     
            self.__fill_canvas()
     
        def __fill_canvas(self):
            m = monthcalendar(self.year,self.month)
     
            for col in range(len(m[0])):
                for row in range(len(m)):
                    if m[row][col]==0:
                        pass
                    else:
                        if col==self.col:
                            self.c.create_text(
                                col*20+12,row*20+30,
                                text="%2i" % m[row][col],fill='red',tags='day')
                        else:
                            self.c.create_text(
                                col*20+12,row*20+30,
                                text="%2i" % m[row][col],tags='day')
            x=12; y=10
            for i in self.dayArray:
                self.c.create_text(x,y,text=i,fill='blue', tags='day')
                x+=20
     
        def __decreaseYear(self):
            self.c.delete('day')
            self.year -= 1
            self.l.configure(text="%.2i.%i" % (self.month, self.year))
            self.__fill_canvas()
     
        def __decrease(self):
            self.c.delete('day')
            if self.month == 1:
                self.year -= 1
                self.month = 12
            else:
                self.month -= 1
            self.l.configure(text="%.2i.%i" % (self.month, self.year))
            self.__fill_canvas()
     
        def __increase(self):
            self.c.delete('day')
            if self.month == 12:
                self.year += 1
                self.month = 1
            else:
                self.month += 1
            self.l.configure(text="%.2i.%i" % (self.month, self.year))
            self.__fill_canvas()
     
        def __increaseYear(self):
            self.c.delete('day')
            self.year += 1
            self.l.configure(text="%.2i.%i" % (self.month, self.year))
            self.__fill_canvas()
     
        def __click(self,event):
            x = self.c.find_closest(event.x,event.y)[0]
            try:
                day = self.c.itemcget(x,'text')
                self.date.set("%.2i.%.2i.%.2i" % (self.year%100,self.month,int(day)))
                self.callback(self.date.get())
     
            except Exception, msg:
                pass
     
    class TkCalendarButton(tk.Button):
        def __init__(self,master,text):
            tk.Button.__init__(self,master,text=text,command=self.toogle)
            self.popup = tk.Toplevel(master)
            year,month = localtime()[0:2]
            self.calendar = TkCalendar(self.popup, year, month, self.callback)
            self.calendar.pack()
            self.calendar.visible = tk.BooleanVar()
            self.hideCalendar()
            self.value = tk.StringVar()
     
        def toogle(self):
            if not self.calendar.visible.get():
                self.showCalendar()
            else:
                self.hideCalendar()
     
        def showCalendar(self):
            self.popup.deiconify()
            self.calendar.visible.set(True)
     
        def hideCalendar(self):
            self.popup.withdraw()
            self.calendar.visible.set(False)
     
        def callback(self, data):
            self['text'] = data
            self.value.set(string.replace(data,'.',''))
            self.hideCalendar()
     
    class MRZBuilder(tkSimpleDialog.Dialog):
     
        def body(self, master):
            self.resizable(False,False)
     
            tk.Label(master,text="Please enter the following information:").grid(
                row=0,column=0, columnspan=2,sticky=tk.E+tk.W,padx=3,pady=2)
            tk.Label(master,text="Passport number:").grid(row=1,column=0,sticky=tk.W)
            tk.Label(master,text="Birth date:").grid(row=2,column=0,sticky=tk.W)
            tk.Label(master,text="Expiry date:").grid(row=3,column=0,sticky=tk.W)
     
            self.passportNum = tk.Entry(master)
            self.passportNum.grid(row=1,column=1,sticky=tk.E+tk.W,padx=3,pady=2)
            self.birth = TkCalendarButton(master,'Select')
            self.birth.grid(row=2,column=1,sticky=tk.E+tk.W,padx=3,pady=2)
            self.expiry = TkCalendarButton(master,'Select')
            self.expiry.grid(row=3,column=1,sticky=tk.E+tk.W,padx=3,pady=2)
     
            return self.passportNum
     
        def validate(self):
            print 'validate'
            try:
                assert len(self.passportNum.get()) > 0, "Please Enter passport number"
                birth = int(self.birth.value.get())
                expiry = int(self.expiry.value.get())
                assert birth < expiry, "expiry date cannot be anterior to birth date"
                return 1
            except Exception, msg:
                print msg
                return 0
     
        def apply(self):
            print 'apply'
            self.result = self.buildMRZ()
     
        def buildMRZ(self):
            print 'build'
            return self.passportNum.get() + self.birth.value.get() + self.expiry.value.get()
     
    if __name__ == '__main__':
        root = tk.Tk()
        dialog = MRZBuilder(root, "Required Information")
        print 'MRZ:', dialog.result
        root.mainloop()

  2. #2
    Membre éclairé
    Avatar de panda31
    Homme Profil pro
    Conseil - Consultant en systèmes d'information
    Inscrit en
    Juin 2003
    Messages
    670
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France

    Informations professionnelles :
    Activité : Conseil - Consultant en systèmes d'information
    Secteur : Conseil

    Informations forums :
    Inscription : Juin 2003
    Messages : 670
    Points : 848
    Points
    848
    Par défaut
    Je suis pas expert mais tu mets
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    self.c.bind('<1>', self.__click)
    Donc quand tu fais '1', tu associes click !
    Je suppose que tu dis de faire la même chose que si tu cliques non ?

    Le problème est que ton click n'existe plus alors. Tu devrais faire plutot

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    self.c.bind('<1>', self.methodeClick)
    où methodeClick est aussi la méthode à associer au click.

    A confirmer...
    Michaël Mary
    Consultant PLM dans une société de conseil toulousaine
    Auditeur CNAM-IPST depuis septembre 2008
    "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
    John F. Woods
    mon cv et mon domaine et mon blog
    Aucune question technique par MP, svp

Discussions similaires

  1. Problème avec Search Dialog
    Par ecplusplus dans le forum Composants graphiques
    Réponses: 0
    Dernier message: 22/06/2015, 17h11
  2. Problèmes avec Dialog
    Par m.klaury dans le forum Composants graphiques
    Réponses: 2
    Dernier message: 28/09/2009, 11h13
  3. Problème avec Application.Dialogs(xlDialogSaveAs).Show
    Par melouille56 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 20/12/2007, 18h54
  4. Problème avec contrôle common Dialog 6.0
    Par electrosat03 dans le forum Access
    Réponses: 1
    Dernier message: 03/03/2007, 00h51
  5. problème avec la boite de dialog d'impression
    Par hrp dans le forum C++Builder
    Réponses: 4
    Dernier message: 26/01/2005, 16h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo