Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.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 对if语句有问题_Python_Tkinter - Fatal编程技术网

Python 对if语句有问题

Python 对if语句有问题,python,tkinter,Python,Tkinter,对于我输入的if语句,它只会显示lose语句,即使它是正确的 我不确定我写声明的方式是否正确 我试图使它,当按下开始时,两个标签将显示一个介于1到21之间的数字 另外,如果可能的话,我想在按下hit按钮时,在标签上添加一个数字。例如,按hit将加10+5,然后显示总数 位于TTY类: if score>deal:比较两个tkinter标签对象,而不是score和deal的值。在进行比较之前,请尝试获取标签的值并将其转换为整数 if int(score['text']) > int(deal

对于我输入的if语句,它只会显示lose语句,即使它是正确的

我不确定我写声明的方式是否正确

我试图使它,当按下开始时,两个标签将显示一个介于1到21之间的数字

另外,如果可能的话,我想在按下hit按钮时,在标签上添加一个数字。例如,按hit将加10+5,然后显示总数

位于TTY类:

if score>deal:比较两个tkinter标签对象,而不是score和deal的值。在进行比较之前,请尝试获取标签的值并将其转换为整数

if int(score['text']) > int(deal['text']):
来帮助你回答其他问题。 要选择介于1和21之间的随机数,请使用python随机模块中包含的randint函数,请参见下面的代码。我添加了一个新的随机函数,该函数将在页面创建后调用,以随机选择交易和得分的值

使用hit按钮,我添加了一个新的函数hit,它将获取当前分数,并向其添加另一个随机值

import tkinter as tk
from random import randint

k = 10
Q = 10
J = 10
A = 11 or 1

class WINDOW(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Memory") #sets the window title

        container = tk.Frame(self)#Name of frame to refer to
        container.pack(side="top", fill="both", expand=True)#size of window
        container.grid_rowconfigure(0, weight=4)#size of window
        container.grid_columnconfigure(0, weight=4)

        self.frames = {}
        for F in (MainMenu, tty): 
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("MainMenu") 

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = 'white')

        label = tk.Label(self, text="Memory",font=(15),
                            borderwidth=5, relief="solid")
        label.pack(side="top", fill="y", pady=15, padx=270)
        label.pack(fill="both")

        button1 = tk.Button(self, text="Start", relief="solid",
                            borderwidth=5,width=30,
                            font=(17),command=lambda: 
                                controller.show_frame("tty"))
        button1.pack()

        button3 = tk.Button(self, 
        text="Quit",relief="solid",borderwidth=4,width=30,font=(17),command = quit)
        button3.place(x="420", y ="50")
        button3.pack()

class tty(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = "white")



        self.deal = tk.Label(self, text="18", font=(18))
        self.deal.pack(side="top", fill="y", pady=15, padx=270)

        self.score = tk.Label(self, text="19", font=(18))
        self.score.pack()

        f = tk.Frame(self)
        button1 = tk.Button(f,borderwidth=5, text="stand", font=(18),command = self.win)#This is the button that i want to display the label
        button1.grid(row=0,column=0) 

        button2 = tk.Button(f, text="Hit",borderwidth=5, font=(18),command = self.hit)
        button2.grid(row=0,column=1)
        f.pack(side="bottom")

        button3 = tk.Button(self, text="Quit", font=(18))
        button3.pack(side="right", pady=50)

        self.randomise()

    def randomise(self):
        self.deal['text'] = str(randint(1,21))
        self.score['text'] = str(randint(1,21))

    def hit(self):
        current_score = int(self.score['text'])
        new_score = current_score + randint(1,21)
        self.score['text'] = str(new_score)


    def win(self):
            if int(self.score['text']) > int(self.deal['text']):
                tts = tk.Label(self, text="win", font=(20))
                tts.pack()
            else:
                lose = tk.Label(self, text="lose", font=(10))
                lose.pack() #The if statement

if __name__ == "__main__":
    app = WINDOW()
    app.geometry("800x400")

    app.mainloop()

你应该先修正缩进。
import tkinter as tk
from random import randint

k = 10
Q = 10
J = 10
A = 11 or 1

class WINDOW(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Memory") #sets the window title

        container = tk.Frame(self)#Name of frame to refer to
        container.pack(side="top", fill="both", expand=True)#size of window
        container.grid_rowconfigure(0, weight=4)#size of window
        container.grid_columnconfigure(0, weight=4)

        self.frames = {}
        for F in (MainMenu, tty): 
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("MainMenu") 

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = 'white')

        label = tk.Label(self, text="Memory",font=(15),
                            borderwidth=5, relief="solid")
        label.pack(side="top", fill="y", pady=15, padx=270)
        label.pack(fill="both")

        button1 = tk.Button(self, text="Start", relief="solid",
                            borderwidth=5,width=30,
                            font=(17),command=lambda: 
                                controller.show_frame("tty"))
        button1.pack()

        button3 = tk.Button(self, 
        text="Quit",relief="solid",borderwidth=4,width=30,font=(17),command = quit)
        button3.place(x="420", y ="50")
        button3.pack()

class tty(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(background = "white")



        self.deal = tk.Label(self, text="18", font=(18))
        self.deal.pack(side="top", fill="y", pady=15, padx=270)

        self.score = tk.Label(self, text="19", font=(18))
        self.score.pack()

        f = tk.Frame(self)
        button1 = tk.Button(f,borderwidth=5, text="stand", font=(18),command = self.win)#This is the button that i want to display the label
        button1.grid(row=0,column=0) 

        button2 = tk.Button(f, text="Hit",borderwidth=5, font=(18),command = self.hit)
        button2.grid(row=0,column=1)
        f.pack(side="bottom")

        button3 = tk.Button(self, text="Quit", font=(18))
        button3.pack(side="right", pady=50)

        self.randomise()

    def randomise(self):
        self.deal['text'] = str(randint(1,21))
        self.score['text'] = str(randint(1,21))

    def hit(self):
        current_score = int(self.score['text'])
        new_score = current_score + randint(1,21)
        self.score['text'] = str(new_score)


    def win(self):
            if int(self.score['text']) > int(self.deal['text']):
                tts = tk.Label(self, text="win", font=(20))
                tts.pack()
            else:
                lose = tk.Label(self, text="lose", font=(10))
                lose.pack() #The if statement

if __name__ == "__main__":
    app = WINDOW()
    app.geometry("800x400")

    app.mainloop()