Advertisement
Guest User

Python article 30

a guest
Mar 25th, 2012
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.75 KB | None | 0 0
  1. # test1.py
  2. #! /usr/bin/env python
  3. # -*- python -*-
  4.  
  5. import sys
  6.  
  7. py2 = py30 = py31 = False
  8. version = sys.hexversion
  9. if version >= 0x020600F0 and version < 0x03000000 :
  10.     py2 = True    # Python 2.6 ou 2.7
  11.     from Tkinter import *
  12.     import ttk
  13. elif version >= 0x03000000 and version < 0x03010000 :
  14.     py30 = True
  15.     from tkinter import *
  16.     import ttk
  17. elif version >= 0x03010000:
  18.     py31 = True
  19.     from tkinter import *
  20.     import tkinter.ttk as ttk
  21. else:
  22.     print ("""
  23.    Votre version de Python ne supporte pas les widgets ttk...
  24.    Il faut une version >= 2.6 pour executer les modules PAGE.
  25.    """)
  26.     sys.exit()
  27.  
  28. def point_depart_graphique():
  29.     '''Point de depart lorsque le module est la routine principale.'''
  30.     global val, w, root
  31.     root = Tk()
  32.     root.title('Form_Test')
  33.     root.geometry('280x186+474+263')
  34.     w = Form_Test (root)
  35.     init()
  36.     root.mainloop()
  37.  
  38. w = None
  39. def create_Form_Test (root):
  40.     '''Point de depart lorsque le module est importe par un autre programme.'''
  41.     global w, w_win
  42.     if w: # on a une seule instance de fenetre
  43.         return
  44.     w = Toplevel (root)
  45.     w.title('Form_Test')
  46.     w.geometry('280x186+474+263')
  47.     w_win = Form_Test (w)
  48.     init()
  49.     return w_win
  50.  
  51. def destroy_Form_Test ():
  52.     global w
  53.     w.destroy()
  54.     w = None
  55.  
  56. def ClicBouton1(p1) :
  57.     sys.exit()
  58.  
  59. def init():
  60.     pass
  61.  
  62. class Form_Test:
  63.     def __init__(self, principale=None):
  64.         # regle le fond de fenetre pour correspondre au style actuel
  65.         style = ttk.Style()
  66.         theme = style.theme_use()
  67.         default = style.lookup(theme, 'background')
  68.         principale.configure(background=default)
  69.  
  70.         self.Bouton1 = Button (principale)
  71.         self.Bouton1.place(relx=0.43,rely=0.38,height=27,width=49)
  72.         self.Bouton1.configure(activebackground="#f9f9f9")
  73.         self.Bouton1.configure(text='''Quitter''')
  74.         self.Bouton1.bind('<Button-1>',ClicBouton1)
  75.  
  76. if __name__ == '__main__':
  77.     point_depart_graphique()
  78.  
  79. #############################################
  80. # demos.py
  81. #! /usr/bin/env python
  82. # -*- python -*-
  83.  
  84. import sys
  85.  
  86. py2 = py30 = py31 = False
  87. version = sys.hexversion
  88. if version >= 0x020600F0 and version < 0x03000000 :
  89.     py2 = True    # Python 2.6 or 2.7
  90.     from Tkinter import *
  91.     import ttk
  92. elif version >= 0x03000000 and version < 0x03010000 :
  93.     py30 = True
  94.     from tkinter import *
  95.     import ttk
  96. elif version >= 0x03010000:
  97.     py31 = True
  98.     from tkinter import *
  99.     import tkinter.ttk as ttk
  100. else:
  101.     print ("""
  102.    You do not have a version of python supporting ttk widgets..
  103.    You need a version >= 2.6 to execute PAGE modules.
  104.    """)
  105.     sys.exit()
  106.  
  107. def point_depart_graphique():
  108.     '''Starting point when module is the main routine.'''
  109.     global val, w, root
  110.     root = Tk()
  111.     root.title('Demos')
  112.     root.geometry('510x230+283+147')
  113.     set_Tk_var()
  114.     w = Demos (root)
  115.     init()
  116.     root.mainloop()
  117.  
  118. w = None
  119. def create_Demos (root):
  120.     '''Starting point when module is imported by another program.'''
  121.     global w, w_win
  122.     if w: # So we have only one instance of window.
  123.         return
  124.     w = Toplevel (root)
  125.     w.title('Demos')
  126.     w.geometry('510x230+283+147')
  127.     set_Tk_var()
  128.     w_win = Demos (w)
  129.     init()
  130.     return w_win
  131.  
  132. def destroy_Demos ():
  133.     global w
  134.     w.destroy()
  135.     w = None
  136.  
  137. def set_Tk_var():
  138.     # These are Tk variables used passed to Tkinter and must be
  139.     # defined before the widgets using them are created.
  140.     global rbc1
  141.     rbc1 = StringVar()
  142.  
  143.     global rbc2
  144.     rbc2 = StringVar()
  145.  
  146. def btnExitClicked(p1) :
  147.     sys.exit()
  148.  
  149. def btnNormalClicked(p1) :
  150.     print "Normal Button Clicked"
  151.  
  152. def btnSunkenClicked(p1) :
  153.     print "Sunken Button Clicked"
  154.  
  155. def init():
  156.     pass
  157.  
  158. class Demos:
  159.     def __init__(self, master=None):
  160.         # Set background of toplevel window to match
  161.         # current style
  162.         style = ttk.Style()
  163.         theme = style.theme_use()
  164.         default = style.lookup(theme, 'background')
  165.         master.configure(background=default)
  166.  
  167.         self.Frame1 = Frame (master)
  168.         self.Frame1.place(relx=0.02,rely=0.04,relheight=0.33,relwidth=0.97)
  169.         self.Frame1.configure(relief=GROOVE)
  170.         self.Frame1.configure(borderwidth="2")
  171.         self.Frame1.configure(relief="groove")
  172.  
  173.         self.Label1 = Label (self.Frame1)
  174.         self.Label1.place(relx=0.02,rely=0.13,height=19,width=51)
  175.         self.Label1.configure(activebackground="#f9f9f9")
  176.         self.Label1.configure(text='''Buttons''')
  177.  
  178.         self.Button1 = Button (self.Frame1)
  179.         self.Button1.place(relx=0.34,rely=0.4,height=27,width=69)
  180.         self.Button1.configure(activebackground="#f9f9f9")
  181.         self.Button1.configure(text='''Normal''')
  182.         self.Button1.bind('<Button-1>',btnNormalClicked)
  183.  
  184.         self.Button2 = Button (self.Frame1)
  185.         self.Button2.place(relx=0.61,rely=0.4,height=27,width=72)
  186.         self.Button2.configure(activebackground="#f9f9f9")
  187.         self.Button2.configure(text='''Sunken''')
  188.         self.Button2.bind('<Button-1>',btnSunkenClicked)
  189.  
  190.         self.Frame2 = Frame (master)
  191.         self.Frame2.place(relx=0.02,rely=0.39,relheight=0.33,relwidth=0.97)
  192.         self.Frame2.configure(relief=GROOVE)
  193.         self.Frame2.configure(borderwidth="2")
  194.         self.Frame2.configure(relief="groove")
  195.  
  196.         self.Radiobutton1 = Radiobutton (self.Frame2)
  197.         self.Radiobutton1.place(relx=0.32,rely=0.13,relheight=0.28
  198.                 ,relwidth=0.14)
  199.         self.Radiobutton1.configure(activebackground="#f9f9f9")
  200.         self.Radiobutton1.configure(text='''radio 1''')
  201.         self.Radiobutton1.configure(value="0")
  202.         self.Radiobutton1.configure(variable=rbc1)
  203.  
  204.         self.Radiobutton2 = Radiobutton (self.Frame2)
  205.         self.Radiobutton2.place(relx=0.32,rely=0.4,relheight=0.28,relwidth=0.14)
  206.  
  207.         self.Radiobutton2.configure(activebackground="#f9f9f9")
  208.         self.Radiobutton2.configure(text='''radio 2''')
  209.         self.Radiobutton2.configure(value="1")
  210.         self.Radiobutton2.configure(variable=rbc1)
  211.  
  212.         self.Radiobutton3 = Radiobutton (self.Frame2)
  213.         self.Radiobutton3.place(relx=0.61,rely=0.13,relheight=0.28
  214.                 ,relwidth=0.14)
  215.         self.Radiobutton3.configure(activebackground="#f9f9f9")
  216.         self.Radiobutton3.configure(text='''radio 3''')
  217.         self.Radiobutton3.configure(value="0")
  218.         self.Radiobutton3.configure(variable=rbc2)
  219.  
  220.         self.Radiobutton4 = Radiobutton (self.Frame2)
  221.         self.Radiobutton4.place(relx=0.61,rely=0.4,relheight=0.28,relwidth=0.14)
  222.  
  223.         self.Radiobutton4.configure(activebackground="#f9f9f9")
  224.         self.Radiobutton4.configure(text='''radio 4''')
  225.         self.Radiobutton4.configure(value="1")
  226.         self.Radiobutton4.configure(variable=rbc2)
  227.  
  228.         self.Label2 = Label (self.Frame2)
  229.         self.Label2.place(relx=0.02,rely=0.13,height=19,width=88)
  230.         self.Label2.configure(activebackground="#f9f9f9")
  231.         self.Label2.configure(text='''Radio Buttons''')
  232.  
  233.         self.Button3 = Button (master)
  234.         self.Button3.place(relx=0.45,rely=0.83,height=27,width=49)
  235.         self.Button3.configure(activebackground="#f9f9f9")
  236.         self.Button3.configure(text='''Exit''')
  237.         self.Button3.bind('<Button-1>',btnExitClicked)
  238.  
  239. if __name__ == '__main__':
  240.     point_depart_graphique()
  241.  
  242. #############################################
  243. # demos.py
  244. #! /usr/bin/env python
  245. # -*- python -*-
  246.  
  247. import sys
  248.  
  249. py2 = py30 = py31 = False
  250. version = sys.hexversion
  251. if version >= 0x020600F0 and version < 0x03000000 :
  252.     py2 = True    # Python 2.6 ou 2.7
  253.     from Tkinter import *
  254.     import ttk
  255. elif version >= 0x03000000 and version < 0x03010000 :
  256.     py30 = True
  257.     from tkinter import *
  258.     import ttk
  259. elif version >= 0x03010000:
  260.     py31 = True
  261.     from tkinter import *
  262.     import tkinter.ttk as ttk
  263. else:
  264.     print ("""
  265.    Votre version de Python ne supporte pas les widgets ttk...
  266.    Il faut une version >= 2.6 pour executer les modules PAGE.
  267.    """)
  268.     sys.exit()
  269.  
  270. def point_depart_graphique():
  271.     '''Point de depart lorsque le module est la routine principale.'''
  272.     global val, w, root
  273.     root = Tk()
  274.     root.title('Demos')
  275.     root.geometry('510x230+283+147')
  276.     regle_variables_Tk()
  277.     w = Demos (root)
  278.     init()
  279.     root.mainloop()
  280.  
  281. w = None
  282. def create_Demos (root):
  283.     '''Point de depart lorsque le module est importe par un autre programme.'''
  284.     global w, w_win
  285.     if w: # on a une seule instance de fenetre
  286.         return
  287.     w = Toplevel (root)
  288.     w.title('Demos')
  289.     w.geometry('510x230+283+147')
  290.     regle_variables_Tk()
  291.     w_win = Demos (w)
  292.     init()
  293.     return w_win
  294.  
  295. def destroy_Demos ():
  296.     global w
  297.     w.destroy()
  298.     w = None
  299.  
  300. def regle_variables_Tk():
  301.     # Ce sont les variables Tk utilisees et passees a Tkinter
  302.     # Elles doivent etre definies avant de creer les widgets qui les utilisent
  303.     global rbc1
  304.     rbc1 = StringVar()
  305.  
  306.     global rbc2
  307.     rbc2 = StringVar()
  308.  
  309. def ClicBtnQuitter(p1) :
  310.     sys.exit()
  311.  
  312. def clicBtnNormal(p1) :
  313.     print "Clic bouton Normal"
  314.  
  315. def clicBtnCreux(p1) :
  316.     print "Clic bouton Creux"
  317.  
  318. def init():
  319.     pass
  320.  
  321. class Demos:
  322.     def __init__(self, principale=None):
  323.         # regle le fond de fenetre pour correspondre au style actuel
  324.         style = ttk.Style()
  325.         theme = style.theme_use()
  326.         default = style.lookup(theme, 'background')
  327.         principale.configure(background=default)
  328.  
  329.         self.Fenetre1 = Frame (principale)
  330.         self.Fenetre1.place(relx=0.02,rely=0.04,relheight=0.33,relwidth=0.97)
  331.         self.Fenetre1.configure(relief=GROOVE)
  332.         self.Fenetre1.configure(borderwidth="2")
  333.         self.Fenetre1.configure(relief="groove")
  334.  
  335.         self.Label1 = Label (self.Fenetre1)
  336.         self.Label1.place(relx=0.02,rely=0.13,height=19,width=51)
  337.         self.Label1.configure(activebackground="#f9f9f9")
  338.         self.Label1.configure(text='''Boutons''')
  339.  
  340.         self.Bouton1 = Button (self.Fenetre1)
  341.         self.Bouton1.place(relx=0.34,rely=0.4,height=27,width=69)
  342.         self.Bouton1.configure(activebackground="#f9f9f9")
  343.         self.Bouton1.configure(text='''Normal''')
  344.         self.Bouton1.bind('<Button-1>',clicBtnNormal)
  345.  
  346.         self.Bouton2 = Button (self.Fenetre1)
  347.         self.Bouton2.place(relx=0.61,rely=0.4,height=27,width=72)
  348.         self.Bouton2.configure(activebackground="#f9f9f9")
  349.         self.Bouton2.configure(text='''Creux''')
  350.         self.Bouton2.bind('<Button-1>',clicBtnCreux)
  351.  
  352.         self.Frame2 = Frame (principale)
  353.         self.Frame2.place(relx=0.02,rely=0.39,relheight=0.33,relwidth=0.97)
  354.         self.Frame2.configure(relief=GROOVE)
  355.         self.Frame2.configure(borderwidth="2")
  356.         self.Frame2.configure(relief="groove")
  357.  
  358.         self.BoutonRadio1 = Radiobutton (self.Frame2)
  359.         self.BoutonRadio1.place(relx=0.32,rely=0.13,relheight=0.28
  360.                 ,relwidth=0.14)
  361.         self.BoutonRadio1.configure(activebackground="#f9f9f9")
  362.         self.BoutonRadio1.configure(text='''radio 1''')
  363.         self.BoutonRadio1.configure(value="0")
  364.         self.BoutonRadio1.configure(variable=rbc1)
  365.  
  366.         self.BoutonRadio2 = Radiobutton (self.Frame2)
  367.         self.BoutonRadio2.place(relx=0.32,rely=0.4,relheight=0.28,relwidth=0.14)
  368.  
  369.         self.BoutonRadio2.configure(activebackground="#f9f9f9")
  370.         self.BoutonRadio2.configure(text='''radio 2''')
  371.         self.BoutonRadio2.configure(value="1")
  372.         self.BoutonRadio2.configure(variable=rbc1)
  373.  
  374.         self.Radiobutton3 = Radiobutton (self.Frame2)
  375.         self.Radiobutton3.place(relx=0.61,rely=0.13,relheight=0.28
  376.                 ,relwidth=0.14)
  377.         self.Radiobutton3.configure(activebackground="#f9f9f9")
  378.         self.Radiobutton3.configure(text='''radio 3''')
  379.         self.Radiobutton3.configure(value="0")
  380.         self.Radiobutton3.configure(variable=rbc2)
  381.  
  382.         self.Radiobutton4 = Radiobutton (self.Frame2)
  383.         self.Radiobutton4.place(relx=0.61,rely=0.4,relheight=0.28,relwidth=0.14)
  384.  
  385.         self.Radiobutton4.configure(activebackground="#f9f9f9")
  386.         self.Radiobutton4.configure(text='''radio 4''')
  387.         self.Radiobutton4.configure(value="1")
  388.         self.Radiobutton4.configure(variable=rbc2)
  389.  
  390.         self.Label2 = Label (self.Frame2)
  391.         self.Label2.place(relx=0.02,rely=0.13,height=19,width=88)
  392.         self.Label2.configure(activebackground="#f9f9f9")
  393.         self.Label2.configure(text='''Boutons Radio''')
  394.  
  395.         self.Button3 = Button (principale)
  396.         self.Button3.place(relx=0.45,rely=0.83,height=27,width=49)
  397.         self.Button3.configure(activebackground="#f9f9f9")
  398.         self.Button3.configure(text='''Exit''')
  399.         self.Button3.bind('<Button-1>',ClicBtnQuitter)
  400.  
  401. if __name__ == '__main__':
  402.     point_depart_graphique()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement