在Tkinter中调用另一个函数

时间:2018-03-23 12:20:44

标签: python-3.x tkinter

这是我第一次制作这么大的项目(这是一个小部分),这也是我第一次尝试使用Tkinter,所以我对它知之甚少。当我尝试使用第一个函数game()时,它不会等待用户输入。如果用户想要(我们应该能够选择退出而不是直接退出或进入无限循环),我就不能进入下一个函数takeitem() 我试图使用返回函数,但这只会让Tkinter崩溃。我在此之后使用if语句尝试了Command_Line.bind("<Return>", takeitem)

Command_Line.bind("<Return>", takeitem)
    if lowerResponse == "yes"

如果我在break之后没有使用if lowerResponse == "yes",则代码会进入无限循环,因此不会显示任何内容。

下面是我的代码的一部分,它将显示我的输入函数以及项目应该运行的其他函数。

我一直在查看同样的错误2天,但在对youtube进行一些研究之后仍然不了解如何修复它。任何帮助将不胜感激。如果你想知道任何其他内容或更具体的内容,请告诉我,我会添加

from chancecheck import *#this imports anothe file i created called chance 
check
from fightchance import *
import os
import time
import tkinter as tk
from time import sleep

root = tk.Tk()

root.columnconfigure(0, weight = 1)
root.rowconfigure(0, weight = 1)
root.rowconfigure(1, weight = 0)

Main_Textbox = tk.Text(root, width = 83, height = 20)
Main_Textbox.grid(row=0, column=0, sticky="nsew")

Command_Line = tk.Entry(root)
Command_Line.grid(row=1, column=0, sticky="ew")
Command_Line.config(background = "black", foreground = "white")

def start_text(): #used to start the program
start_command = " do you wish to start the game and begin your journey  \n"
Main_Textbox.delete(1.0, "end")
Main_Textbox.insert("end", start_command)

def update_textbox(x): # this is used to update the text box by clearing what is in the command line and adding new text to the output textbox
Main_Textbox.insert("end", x)
Main_Textbox.see("end")
Command_Line.delete(0, "end")

def process_commands(Event=None):
game()

def lowerStringVar(var):
var=var.lower()
return var

def game():   
weapons=[]#list that i will assign weapons to
inv = []#player inventory for minor lowerResponses
loopa = False
loop = 0
dead = False
begin = True
takeitem = False
gold=0#sets the gold of the player to 0
low = 100
   # while dead == False:

    if begin == True:#just a simple starting sequence to introduce the player to the game
        response = Command_Line.get()
        commands()     
        lowerResponse = lowerStringVar(response)
        if lowerResponse == "yes" or  lowerResponse =="yeah" or lowerResponse == "ye" or lowerResponse== "y":
            update_textbox (" a wise choice \n")
            begin = False
        elif lowerResponse == "commands":
            update_textbox (" take that as a yes then \n")
            commands()
            begin = False
        else:
            update_textbox ("cant say im not disapointed \n")
            update_textbox ("ah well you will be back with time\n")
           # quit(1)
            break

#user is asked if they would like to start with an item()
def takeitem ():
   # while takeitem == False:
    displayintro()
    update_textbox(" would you like to start with an item  ")
    response = Command_Line.get()
    lowerResponse = lowerStringVar(response)
    if lowerResponse == "yes"  or lowerResponse == "yeah"  or lowerResponse == "ye" or lowerResponse =="y":
        takeitem = True
        update_textbox(" well then what would you like")
        update_textbox(" the pendant,the dagger,the gold or the key  ")
        response = Command_Line.get()
        lowerResponse = lowerStringVar(response)
        if lowerResponse == "gold" or lowerResponse == "the gold":
            gold = gold + 200
            update_textbox (" gold it is then")
            loop = False
        elif lowerResponse == "dagger" or lowerResponse == "rusted dagger" or lowerResponse == "small rusted dagger":
            update_textbox ("you take the dagger")
            weapons.append("dagger")
            loop = False
        elif lowerResponse =="key" or lowerResponse == "thief" or lowerResponse == "key of theif" or lowerResponse == "the key":
            update_textbox (" you take the key")
            inv.append("key")
        elif lowerResponse =="pendant" or lowerResponse == "pendant of old gods " or lowerResponse == "old gods" or lowerResponse == "gods":
            update_textbox (" you take the pendant")
            inv.append("pendant")
            loop = False
    elif lowerResponse == "no" or lowerResponse == "nope" or lowerResponse == "n":
        takeitem = True
        update_textbox("well on your way then")
        loop = False
    elif lowerResponse =="commands":
         update_textbox (" no you're not getting them")
         update_textbox ("i have told you what to do and you shouldnt need help with this")
         takeitem = False


    else:
        takeitem == False
        update_textbox("sigh looks like we are going to go through this again then")


Command_Line.bind("<Return>", process_commands)
start_text()
root.mainloop()

0 个答案:

没有答案