Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter中使用背景图像后按钮错位_Python_Python 3.x_Tkinter_Tkinter Canvas - Fatal编程技术网

Python 在Tkinter中使用背景图像后按钮错位

Python 在Tkinter中使用背景图像后按钮错位,python,python-3.x,tkinter,tkinter-canvas,Python,Python 3.x,Tkinter,Tkinter Canvas,每当我使用背景图像运行此代码时,按钮网格就会放错位置并被推向底部。幸运的是,当没有添加背景时,它可以正常工作。我希望它们在执行时覆盖背景。下面添加了供参考的图片。非常感谢你的帮助 # importing the module import tkinter.messagebox from tkinter import * import random # importing the module # initialising tkinter class window(Frame): de

每当我使用背景图像运行此代码时,按钮网格就会放错位置并被推向底部。幸运的是,当没有添加背景时,它可以正常工作。我希望它们在执行时覆盖背景。下面添加了供参考的图片。非常感谢你的帮助

# importing the module
import tkinter.messagebox
from tkinter import *
import random
# importing the module

# initialising tkinter
class window(Frame):

    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.master = master
# initialising tkinter

# creating the window
root = Tk()
app = window(root)
root.geometry("630x630")
root.title('Odd Even Game')

C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "BG.png")
background_label = Label(root,image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()

frame = Frame(root)
frame.pack()
# creating the window

# image
level_1e = "p1.png"
level_1o = "pe.png"
level_2e = "r1.png"
level_2o = "re.png"

# image   
def create_cards(odd_image,even_image,next_level,fno,order,suc,err,w,h):
    rx = random.randint(0,order-1)
    ry = random.randint(0,order-1)
    
    for i in range(0,order):
        for j in range(0,order):
            if i == rx and j == ry:
                create_button(i,j,suc,odd_image,next_level,fno,odd_image,w,h)
            else:
                create_button(i,j,err,even_image,next_level,fno,odd_image,w,h)
  
def second_level(fno):
    fno.pack_forget()
    frame2 = Frame(root)
    frame2.pack()
    suc = "Congratulations! You have cleared level 2..Keep Going Buddy!"
    err = "Wrong Answer..Don't give up yet!"
    create_cards(level_2o,level_2e,final_level,frame2,4,suc,err,157.5,157.5)

def final_level(fno):
    fno.pack_forget()
    root.geometry("700x700")
    ap = App(root)


# creating a button function
def create_button(x,y,msg,picture,next_level,fno,odd,w,h):
    if picture == odd:
        image = PhotoImage(file=picture)
        click = Button(fno, image=image, width= w, height=h, bd = 0,command = lambda : [score_update(),next_level(fno),tkinter.messagebox.showinfo( "Odd One Out Project",msg)])
        click.image = image
        click.grid( row = x, column = y)
        
    else:
        image = PhotoImage(file=picture)
        click = Button(fno, image=image, width= w, height=h, bd = 0,command = lambda : [next_level(fno),tkinter.messagebox.showinfo( "Odd One Out Project",msg)])
        click.image = image
        click.grid( row = x, column = y)

# creating a button function

def create_frame(fno):
    root.geometry("630x630")
    fno.pack_forget()
    frame = Frame(root)
    frame.pack()
    suc = "Congratulations! You have cleared level 1..Time to increas[![enter image description here][1]][1]e the difficulty!"
    err = "Wrong Answer..Please Try again !!"
    create_cards(level_1o,level_1e,second_level,frame,3,suc,err,200,200)

def intro():
    root.geometry("630x630")
    frame0 = Frame(root)
    frame0.pack()
    click = Button(frame0,text="Start!" ,command = lambda [create_frame(frame0),tkinter.messagebox.showinfo( "Odd One Out Project","The game has begun!!")])
    click.pack()

intro()

# starting the widget
root.mainloop()
# starting the widget

第一个图像是错误。第二个图像是所需的输出。 注意:我还是Python和Tkinter的初学者,因此各种术语和方法可能超出了我的范围。如能考虑,将不胜感激


如果需要,您可能知道这是一个tkinter项目,用于从*a网格中挑选奇数图像。

我自己得到了答案,因此将分享以备将来使用

C = Canvas(root, bg="blue", height=250, width=300)
此部件绘制250*300尺寸的画布,因此不允许按钮在其上绘制

把它改成

C = Canvas(root, bg="blue", height=0, width=0)
为了达到预期的效果