我最近開始學習 python 和 Tkinter。我大約一個月前開始。但是,讓我們進入正題。我一直在制作一個隨機軟體(所以不要模擬),現在已經有 1 天了,代碼已經準備好了,但是我仍然需要自己制作應用程式,所以我一直在做,但是在制作背景更改按鈕時遇到了問題。我制作了將背景更改為顏色或圖片的按鈕。
好吧,正常的顏色效果很好,但影像看起來很奇怪。我從按鈕中出現了影像,但問題是,當我點擊按鈕時,影像顯示在文本、條目等上方。
適用于不改變圖片的“代碼修復” -->
Tkinter.Label(視窗,i=bgimg,復合=Tkinter.CENTER).pack()
我還沒有看到很多人有這個問題。我看到了一個,并且有效(如果我有一個不可更改的背景),但是因為我有按鈕,所以代碼并不關心它。(有助于保持圖片不變的代碼。)
請有人幫忙,也許可以在 Discord 上加我,因為我認為我以后也需要幫助 :)
哦,這是代碼->
import tkinter as tk
import tkinter
window=Tk()
bgimg= tk.PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")
btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)
def myClick():
myLabel = Label(window, window.configure(bg='red'))
myLabel.pack()
btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)
def myClick():
myLabel = Label(window, window.configure(bg='blue'))
myLabel.pack()
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)
def myClick():
myLabel = Label(window, window.configure(bg='green'))
myLabel.pack()
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)
def myClick():
myLabel = Label(window, window.configure(bg='orchid'))
myLabel.pack()
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)
#Here is the problem, and here is the code that put the picture like it should UNDER the text (if i put the code to the start)(But then it does't change to the other colours)
def myClick():
myLabel= Label(window, i=bgimg, )
myLabel.pack()
btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
#If i now run this code, the picture will come on top of the text. I hope yall get it, im kinda new
#
lbl=Label(window, text=" Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)
txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)
window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500 10 10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')
window.mainloop()
uj5u.com熱心網友回復:
這應該讓你的東西
#import tkinter as tk
from email.mime import image
from tkinter import *
window=Tk()
bgimg= PhotoImage(file = "C:\\Users\\Hoze\\Downloads\\Hoze (1).png")
myLabel= Label(window, i=bgimg )
myLabel.pack()
myLabel.pack_forget()
btn=Button(window, text="Convert", fg='blue', padx=50)
btn.place(x=220, y=380)
def myClick():
myLabel.pack_forget()
window.configure(bg='red')
btn=Button(window, text="Red", fg='red', padx=20, command=myClick)
btn.place(x=520, y=150)
def myClick():
myLabel.pack_forget()
window.configure(bg='blue')
btn=Button(window, text="Blue", fg='blue', padx=19, command=myClick)
btn.place(x=520, y=200)
def myClick():
myLabel.pack_forget()
window.configure(bg='green')
btn=Button(window, text="Green", fg='green', padx=15, command=myClick)
btn.place(x=520, y=300)
def myClick():
myLabel.pack_forget()
window.configure(bg='orchid')
btn=Button(window, text="Default", fg='orchid', padx=11, command=myClick)
btn.place(x=520, y=250)
#Here is the problem, and here is the code that put the picture like it should UNDER the text (if i put the code to the start)(But then it does't change to the other colours)
def myClick():
myLabel.pack()
btn=Button(window, text="Cool", fg='aqua', padx=11, command=myClick)
btn.place(x=520, y=250)
#If i now run this code, the picture will come on top of the text. I hope yall get it, im kinda new
#
lbl=Label(window, text=" Extract a word here, \n that you want make secret!", fg='blue', font=("Helvetica", 16))
lbl.place(x=166 , y=50)
lbl=Label(window, text="Your Secret Word is: ", fg='blue', font=("Helvetica", 10))
lbl.place(x=120, y=300)
txtfld=Entry(window, text="This is Entry Widget", bd=5)
txtfld.place(x=234, y=150)
window.title('Hoze Secret Lenguage Converter')
window.geometry("600x500 10 10")
window.minsize(600, 500)
window.maxsize(600, 500)
window.iconbitmap("C:\\Users\\Hoze\\Music\\Coding\\Project Secret\\favicon.ico")
window.configure(bg='green')
window.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/453021.html
