Redgar Pro
Redgar Pro

Reputation: 51

Creating a UI for a Python Program. Theming with ttk themes for tkinter but it doesn't work for labels and buttons and returns a strange error

Here is the code written so far... The code basically functions as a UI for another Python program. The other python program isn't causing any trouble... No one has been able to assist me with the previous post so I rephrased and reposted...

import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *

app = tk.ThemedTk()
app.get_themes()
app.set_theme("radiance")


app.geometry("400x400")
app.configure(bg='gray')


ex_activate_photo = tk.PhotoImage(file=r"C:\Users\bedga\PycharmProjects\GUIdev\ex_button_active.png") #It underlines PhotoImage
myFont = font.Font(family='Helvetica', size=20, weight='normal')

ttk.Label(app, text='Ex', bg='gray', font=(
    'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')


def ex_activation():
    global pro
    print("Ex")
    pro = subprocess.Popen("python ex.py", shell=True)


def ex_stop():
    global pro
    print("Stopping Program... Please Wait!")
    os.kill(pro.pid, 0)


ex_activation_button = ttk.Button(app, bg='black', image=ex_activate_photo, width=120, height=120, command=ex_activation)


ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)


ex_stop_button['font'] = myFont

app.title("Ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)

# app.mainloop()
while True:
    try:
        app.update()
        app.update_idletasks()
    except KeyboardInterrupt:
        pass

The goal here is to ultimately theme every button (2) and the label at the top. I can then apply similar methods when theming new things in the future. Currently, the PhotoImage is not liking tk and ttk. The program underlines it. One of the buttons being themed is photo-based and the other is text. I have seen successful projects with themed image buttons.

This is the error I get with tk.photoimage

Traceback (most recent call last):
  File "C:/Users/ex/main.py", line 19, in <module>
    ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
AttributeError: module 'ttkthemes.themed_tk' has no attribute 'PhotoImage'

EDIT: This is the error I get for doing

import tkinter as tk
from ttkthemes import themed_tk as tkk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *

I get this error:

Traceback (most recent call last):
  File "C:/Users/ex/main.py", line 19, in <module>
    ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
  File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3539, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3495, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Users\ex\PycharmProjects\ex\ex_button_active.png": no such file or directory

I didn't think ttk themes would have an issue with PhotoImage as a variable because it is a theming library for tkinter. I am very new the GUI development in Python and any help greatly appreciated.

Upvotes: 0

Views: 225

Answers (1)

tayken
tayken

Reputation: 127

You are importing 2 libraries as tk, that's your main problem. First 3 lines of your code is here

import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk

First and third lines have as tk so the latest one is taking over. The error message points to this as well. You should rename one of them.

Upvotes: 2

Related Questions