[Python] Python Tkinter image labels displaying background colour instead of the images

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    I'm a rookie programmer looking to develop a sort of "surface generator" for funzies. I have a problem though, when generating all of the tiles present in the script, instead of it using the 19x19 tiles I saved as pngs, it merely displays background colour, in this case being transparent.

    And I have no idea why. Would any kind soul look at my code and tell me why is this happening? The function causing this issue is the importtiles function.

    # IMPORTS
    import tkinter as tk
    from tkinter import PhotoImage
    import random

    #DIVIDE GREENHOUSE POWER BY 60 TO OBTAIN DESIRED TEMPERATURE !!

    #DEFINE
    def importtiles():
    count = 0
    c1 = 0
    c2 = 0
    f0 = 'andesite.png'
    f1 = 'sand.png'
    for i in range(1, 1008):
    np = open('PlanetSurface.txt', 'r')
    #print(i)
    material = np.readline(i)
    length = len(material)
    if length > 1:
    material = material[:-1]
    print(material)
    if material == '1':
    f = 'andesite.png'
    elif material == '0':
    f = 'sand.png'
    else:
    input('Error')
    #Intentionally crashes the script
    gorbashovodarasboda = 888 + 'webiwabo'
    if count == 42:
    c2 = c2 + 19
    c1 = 0
    count = 0
    print(i)
    tile = PhotoImage(file=f)
    exec('t'+str(i)+'= tk.Label(g,image=tile,bg="pink")')
    exec('t'+str(i)+'.place(x=c1, y=c2)')
    print(c1)
    c1 = c1 + 19
    count = count + 1
    def newplanet(name):
    np = open('Planet.txt', 'w')
    np.write(name + '\nTemperature\n2000\nEquator no greenhouse aiming temperature\n(-7)\nCarbon Dioxide\n450000\nWater Vapor\n50000\nOxygen\n0\nGreenhouse Power\n5371\nCalculated Base Temperature With Greenhouse\n91')
    def surfacegen():
    np = open('PlanetSurface.txt', 'w')
    ti = random.randint(0,1)
    np.write(str(ti)+'\n')
    np = open('PlanetSurface.txt', 'a')
    for i in range(0,1007):
    toss = random.randint(0,3)
    if toss == 3:
    ti = random.randint(0, 1)
    np.write(str(ti)+'\n')
    tilecount = 0
    choice = input('Do you wish to create a new planet or to load an existing one? \n'+'Answer with 1 or 2. ')

    #BEGIN

    if choice == '1':
    name = input("Type in the name of your planet ")
    newplanet(name)
    surfacegen()
    g = tk.Tk()
    g.geometry('800x600')
    g.resizable(0, 0)
    g.configure(background='black')
    g.wm_attributes("-transparentcolor", 'pink')
    importtiles()
    g.update()
    input('end')
    else:
    #ELIF to be added
    print('doo doo')



    I attempted to generate 1000 tiles spread across several rows. The script generated this fine however it generated it with background colours instead of the tile pngs I assigned. I expected tiles with respectively grey and yellow pngs, but got all transparent tiles.

    Continue reading...

Compartilhe esta Página