以下是到目前为止编写的代码。该代码基本上用作另一个Python程序的UI。另一个python程序不会造成任何麻烦……没有人能够帮助我写上一篇文章,所以我改变了措辞并重新发布了……
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这里的目标是最终为每个按钮(2)和顶部的标签创建主题。然后,我可以在将来为新事物创建主题时应用类似的方法。目前,PhotoImage不喜欢tk和ttk。这个程序强调了这一点。其中一个按钮是基于照片的,另一个是文本按钮。我已经看到了主题图像按钮的成功项目。
这是我在使用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:这是我在执行以下操作时得到的错误
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 *我得到了这个错误:
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我不认为ttk themes会把PhotoImage作为一个变量,因为它是tkinter的主题库。我是非常新手的图形用户界面开发的Python和任何帮助非常感谢。
发布于 2020-08-15 01:12:40
你正在导入两个库作为tk,这是你的主要问题。代码的前3行如下所示
import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk第一行和第三行都有as tk,所以最新的那行将会接管。错误消息也指向这一点。您应该重命名其中的一个。
https://stackoverflow.com/questions/63417147
复制相似问题