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 :

[Tkinter] widget Text


Sujet :

Tkinter Python

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut [Tkinter] widget Text
    Bonjour, je suis en train de réaliser un petit editeur de texte pour m'amuser et apprendre un peu plus le python.

    Voici mon code :
    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
    from Tkinter import *
    import tkFileDialog
    from tkFileDialog import *
     
     
    class descxmleditor:
        from Tkinter import Toplevel
        import tkFileDialog
        from tkFileDialog import *
        import os
     
        def __init__(self, root=None):
            #root = root
            self.root = root
            self.menubar = Menu(root)
            self.top = top = Toplevel(root, menu=self.menubar)
            self.product_dir = self.os.getcwd()
            print self.product_dir      
            #call createmenubar
            self.createmenubar()
     
            #call createwidgettext
            self.createText()
     
            print self.root.cget("width")
            print self.root.cget("height")
     
            self.top.protocol("WM_DELETE_WINDOW", self.close)
     
        def close(self):
            self.root.quit()
     
        def createText(self):
            self.textboxFrame = textboxFrame = Frame(self.root)
            self.text = text = Text(textboxFrame)
            rightScrollbar = Scrollbar(textboxFrame, orient=VERTICAL, command=text.yview)
            text.configure(yscrollcommand = rightScrollbar.set)
            bottomScrollbar = Scrollbar(textboxFrame, orient=HORIZONTAL, command=text.xview)
            text.configure(xscrollcommand = bottomScrollbar.set)
            bottomScrollbar.pack(side=BOTTOM, fill=X)
            rightScrollbar.pack(side=RIGHT, fill=Y)
            #text.configure(witdh = self.root.width - 10)
            #text.configure(height = self.root.height - 10)
     
            text.focus_set()
            text.pack(side = TOP)
            textboxFrame.pack(side=TOP)
     
     
        def createmenubar(self):
     
            self.menubar = Menu(self.root)
     
            # create a pulldown menu, and add it to the menu bar
            self.filemenu = Menu(self.menubar, tearoff=0)
            self.filemenu.add_command(label="New",)
            self.filemenu.add_command(label="Open", command = self.open_descxml)
            self.filemenu.add_command(label="Save")
            self.filemenu.add_separator()
            self.filemenu.add_command(label="Exit")
            self.menubar.add_cascade(label="File",menu=self.filemenu)
     
            # create more pulldown menus
            self.editmenu = Menu(self.menubar, tearoff=0)
            self.editmenu.add_command(label="Cut")
            self.editmenu.add_command(label="Copy")
            self.editmenu.add_command(label="Paste")
            self.editmenu.add_separator()
            self.editmenu.add_command(label="Search")
            self.menubar.add_cascade(label="Edit", menu=self.editmenu)
     
            self.toolsmenu = Menu(self.menubar, tearoff =0)
            self.toolsmenu.add_command(label="Test Descxml",command = self.test_descxml)
            self.menubar.add_cascade(label="Tools", menu=self.toolsmenu)
     
            self.helpmenu = Menu(self.menubar, tearoff=0)
            self.helpmenu.add_command(label="About")
            #helpmenu.add_command(label="Trace")
            self.helpmenu.add_command(label="Visit WebSite")
            self.menubar.add_cascade(label="Help", menu=self.helpmenu)
            #self.base_helpmenu_length = self.menudict['help'].index(END)
            # display the menu
            self.root.config(menu=self.menubar)
     
        def open_descxml(self):
            self.opendescxml=tkFileDialog.askopenfilename(filetypes = [("Fichiers descxml", "*.descxml")])
            f=open(self.opendescxml)
            l = 0
            for lines in f:
                l = l+1
                self.text.insert(str(l)+'.0', lines)
     
        def test_descxml(self):
            self.check_path = IntVar()
            self.root_test=Tk()
            self.root_test.title("Valide your descxml file")
            self.test_frame = Frame(self.root_test)
            self.lbl_browsepath = Label(self.test_frame, text="Enter path where is DymanicDialog :")
            self.lbl_browsepath2 = Label(self.test_frame, text="(e.g : \tools\DymanicDialog\DymanicDialog.bat)")
            self.entry_path=Entry(self.test_frame)
            self.entry_path.configure(width =50)
            self.browsepath = Button(self.test_frame, text="...", command = self.browsepath)
            self.ButtonTest = Button(self.test_frame, text = "Test Descxml", command =self.starttest)
            self.SavePath = Checkbutton(self.test_frame,variable=self.check_path, text = " Save path")
            self.SavePath.bind('<Button-1>', self.event_check)
     
            self.lbl_browsepath.grid(row =0)
            self.lbl_browsepath2.grid(row =1, column =0, columnspan =2)
            self.entry_path.grid(row =2, column =0, padx = 10)
            self.browsepath.grid(row =2, column =1, padx =20)
            self.SavePath.grid(row =3, column =0, padx =10)
            self.ButtonTest.grid(row =4)
            self.test_frame.pack(side = TOP)
            self.root_test.mainloop()
     
        def browsepath(self):
            self.path = tkFileDialog.askopenfilename(filetypes = [("All Files", "*.*")])
            if self.entry_path == None:
                self.entry_path.insert(END,self.path)
            else:
                self.entry_path.delete(0,END)
                self.entry_path.insert(END,self.path)
     
        def starttest(self):
            print self.check_path.get()
            if self.check_path.get() == 1:
                print "project saved"
                write_editor_ini()
            else:
                pass
            self.os.system("start " + str(self.entry_path.get()))
     
        def event_check(self, evt):
            print self.check_path.get()
            #if self.check_path.get() == 1:
     
                #self.check_path = 1
            #else:
               #print self.check_path.get()
                #self.check_path = 0
     
     
        def write_editor_ini(self):
            self.g=open(product_dir + "\editor.ini","w+")
            #InstallDir = opInstallDir.replace("/","\\")
            self.g.write("Product_Dir=" + self.product_dir + "\n")
            self.g.write("SavePath=" + self.entry_path.get() +"\n")
            self.g.close()
     
    def main():
        root = Tk()
        root.title("DESCXML Editor")
        root.config(width=300)
        root.config(height=100)
        edit = descxmleditor(root=root)
        root.mainloop()
        #root.destroy()
     
    if __name__ == '__main__':
        main()
    ma premiere question est comment puis-je faire pourque mon widget texte soit agrandi si on agrandi la fenêtre principale?

    ensuite, j'ai mis une scrollbar vertical et horizontal dans mon widget text, mais seule la scrollbar vertical marche. Comment puis-je y remedier?

    Merci de votre aide

  2. #2
    Expert éminent sénior
    Avatar de Guigui_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2002
    Messages
    1 864
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2002
    Messages : 1 864
    Points : 10 067
    Points
    10 067
    Par défaut Re: [Python] widget Text
    Citation Envoyé par t_om84
    ma premiere question est comment puis-je faire pourque mon widget texte soit agrandi si on agrandi la fenêtre principale?
    Au moment où tu pack, utilise les paramètres expand et fill
    expand=True: ton widget s'étend sur tout l'espace utilisé par son parent

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    text.pack(side = TOP, expand=True,fill=BOTH) 
    textboxFrame.pack(side=TOP, expand=True, fill=BOTH)

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    Merci bien, ca marche, je note ça pour l'utiliser plus tard si j'en ai besoin

  4. #4
    Expert éminent sénior
    Avatar de Guigui_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2002
    Messages
    1 864
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Saône et Loire (Bourgogne)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2002
    Messages : 1 864
    Points : 10 067
    Points
    10 067
    Par défaut
    et pour la deuxième question:

    par défaut, wrap=CHAR, tu peux le mettre aussi à WORD

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    Merci beaucoup, ça fonctionne

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    J'ai encore un petit probleme qui me tracasse

    J'affiche un log dans mon widget TEXT, jusque là pas de soucis.
    Dans mon interfca j'ia plusieurs bouton qui lance des fichiers batch. Quand je clique sur un bouton, j'aimerai effacer le contenu du widget.

    Je vous montre mon code

    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
    def clear_batch():
    	global bouton
    	global text
    	text.delete('1.0', END)
     
    def convert_bat():
    	global project_name
    	global basename_inputfile
    	global project_type
     
    	clear_batch()
    	os.system("normalize_" + str(project_type) + ".bat " + str(project_name) + " " + str(basename_inputfile) + " Conversion End" )
    	view_batch()
     
    ### Execute Normalization
    def normalization_bat():
    	global project_name
    	global basename_inputfile
    	global project_type
     
    	clear_batch()
    	os.system("normalize_" + str(project_type) + ".bat " + str(project_name) + " " + str(basename_inputfile) + " Normalization End" )
    	view_batch()
    Extraction_bat et Normalization_bat qont executé lors d'un clik sur un boton. Hors avec ce code, quand je clique sur un bouton, ca ne m'efface pas le contenu et le batch se lance comme meme.
    Ai-je fait une erreur?

  7. #7
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    bon je me reponds tout seul!!!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    def clear_batch(): 
       global bouton 
       global text 
       text.delete('1.0', END) 
       text.update()
    désolé

  8. #8
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2004
    Messages
    37
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2004
    Messages : 37
    Points : 45
    Points
    45
    Par défaut
    hey attention : fichier bat = windows, c est pas très portable comme code. Enfin dépend ce que font tes fichiers mais si possible évite ce genre de chose ; )

  9. #9
    Membre habitué
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    192
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 192
    Points : 160
    Points
    160
    Par défaut
    salut,

    pour l'instant je dévéloppe sous windows, j'apprends le python sur le tas!! Je vais bientot devoir le porter sous unix, donc a ce moment là je ferais des tests au démarrage de l'appli afin de choisir de lancer mes scripts en bat ou sh.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Question de TkInter / Widget text.mark_set
    Par Captain'Flam dans le forum Général Python
    Réponses: 7
    Dernier message: 29/04/2014, 13h57
  2. Text vertical dans Tkinter canvas text
    Par jcgarreau dans le forum Tkinter
    Réponses: 4
    Dernier message: 15/08/2009, 00h29
  3. Widget Text et mise en forme des sélections
    Par Chris33 dans le forum Tkinter
    Réponses: 1
    Dernier message: 20/09/2006, 09h10
  4. colorier une colonne dans un widget text
    Par Chris33 dans le forum Tkinter
    Réponses: 4
    Dernier message: 14/09/2006, 11h20
  5. [tkinter] widget text et scrollbar
    Par jojolapine dans le forum Tkinter
    Réponses: 3
    Dernier message: 29/04/2006, 15h58

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