Python Forum
How to make it shorter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to make it shorter
#1
from tkinter import *
from PIL import ImageTk,Image


def un():
    text1 = int(input("Voir la frise normale(1) ou modifier la frise normale(2)"))
    if text1 == 1:
        frisenormal(),
    if text1 == 2:
        tout()

def frisenormal():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('1NORMALE.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise1():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('2Homo nonhabilis.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise2():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('3Pas le feu.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise3():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('4ne decouvre pas lart.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise4():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('5roue.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise5():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('6ecriture.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise6():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('5roue.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise7():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('6ecriture.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise8():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('7pyramide.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise9():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('8republic ath.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise10():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('9J-C.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise11():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('10boussole.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise12():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('11 christhope colomb.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise13():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('12terreplate.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')


def frise14():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('13 droit de lhomme.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise15():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('14 automobile.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise16():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('15ww1.jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def frise17():
                main = Tk()
                main.geometry("1260x625+100+100")
                main.title('Frise du monde')
                canvas = Canvas(main,width = 1300,height = 650)
                canvas.place(relx=0,rely=0)
                im = Image.open('16ww2 .jpeg')
                canvas.image = ImageTk.PhotoImage(im)
                canvas.create_image(0,0,image = canvas.image,anchor='nw')

def tout():
    with open("ztext.txt", "r") as file_object:
        print(file_object.read())
        tout2()

def tout2():
    texte3 = int(input("choisisez un nombres: "))
        
    if texte3 == 1:
        frise1(),
    if texte3 == 2:
        frise2(),
    if texte3 == 3:
        frise3(),
    if texte3 == 4:
        frise(),
    if texte3 == 5:
        frise4(),
    if texte3 == 6:
        frise5(),
    if texte3 == 7:
        frise6(),
    if texte3 == 8:
        frise7(),
    if texte3 == 9:
        frise8(),
    if texte3 == 10:
        frise9(),
    if texte3 == 11:
        frise10(),
    if texte3 == 12:
        frise11(),
    if texte3 == 13:
        frise12(),
    if texte3 == 14:
        frise13(),
    if texte3 == 15:
        frise14(),
    if texte3 == 16:
        frise15(),
    if texte3 == 17:
        frise16,
    if texte3 == 18:
        frise17,
        return


un()
Hello, I just made a programme that open image depending on the user choice. But i would like to make it shorter, especialy the part where there is "if texte3=" and also the part where I use fonction for image settings. What could I do ?
Reply
#2
Well, fisrst notice that all your friseX functions are the same except for the file name... you can create just one:
def frise(img):
    main = Tk()
    main.geometry("1260x625+100+100")
    main.title('Frise du monde')
    canvas = Canvas(main,width = 1300,height = 650)
    canvas.place(relx=0,rely=0)
    im = Image.open(img)
    canvas.image = ImageTk.PhotoImage(im)
    canvas.create_image(0,0,image = canvas.image,anchor='nw')
And then store the name of the image that is associated to each index in a dictionary:
images = {1: '2Homo nonhabilis.jpeg', 2: '3Pas le feu.jpeg'} # And so on...
So your chain of if's is replaced by:
frise(images[texte3])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020