如何更改显示tkinter小部件和运行函数的顺序



我正在尝试制作一个基本莫尔斯电码翻译器。我希望它首先显示一个窗口,为您提供不同模式的选项。我希望能够选择翻译模式,然后将窗口更改为";翻译屏幕";然后它应该运行translate函数。

相反,当我选择我的模式时,它运行函数,然后一旦函数完成,它就变为";翻译屏幕">

我试过改变事情的运行顺序,但没有任何效果。我不知道该怎么办。而且我使用tkinter的时间不长。

这是在选择模式后更改窗口并运行功能的功能

def choose(selected):
allOff()#this turns off led
if selected.get() == 1:
print("1")
root.title("Translate")
trans.grid(column=2, row=0)#shows a label
backButton.grid(column=2, row=5)# shows a button
againBtn.grid(column=2, row=4)#shows another button
clear() #this is to clear the widgets from the choosing screen
transColorOn() #this just turns on an led
translate()    #this is the translate function 

这是翻译功能

def translate():
global i, LetterToSend
inBruh()
i = 0
while i < len(morse):
if LetterToSend == morse[i]:
print("" + (alphabet[i]))
letterBig = alphabet[1]
break
i += 1

LetterToSend = ""

这是的输入功能

def inBruh():
global LetterToSend
LetterToSend = ""
tempIN = input(". or - , then donen")
while tempIN != "done":
if tempIN == ".":
LetterToSend = "" + LetterToSend + "."
print("" + (LetterToSend))
tempIN = input(". or - , then donen")
elif tempIN == "-":
LetterToSend = "" + LetterToSend + "-"
print("" + (LetterToSend))
tempIN = input(". or - , then donen")
else:
print("plsss valid inputn")
tempIN = input(". or - , then donen")

发送帮助感谢

由于您的问题中缺乏信息,我对代码进行了一些修改,使其能够在中工作

from tkinter import *
def translate():
global i, LetterToSend
inBruh()
'''i = 0
while i < len(morse):
if LetterToSend == morse[i]:
print("" + (alphabet[i]))
letterBig = alphabet[1]
break
i += 1

LetterToSend = ""'''
print('Executed translate')
def inBruh():
global LetterToSend
LetterToSend = ""
#tempIN = input(". or - , then donen")
'''while tempIN != "done":
if tempIN == ".":
LetterToSend = "" + LetterToSend + "."
print("" + (LetterToSend))
tempIN = input(". or - , then donen")
elif tempIN == "-":
LetterToSend = "" + LetterToSend + "-"
print("" + (LetterToSend))
tempIN = input(". or - , then donen")
else:
print("plsss valid inputn")
tempIN = input(". or - , then donen")'''
print('Executed inBruh')
def choose(selected):
global root,base_frame
def clear():
base_frame.pack_forget()
print('Cleared main')
#allOff()#this turns off led
print('LED: RED')
if selected.get() == 1:
print("1")
root.title("Translate")
clear() #this is to clear the widgets from the choosing screen
#trans.grid(column=2, row=0)#shows a label
#backButton.grid(column=2, row=5)# shows a button
#againBtn.grid(column=2, row=4)#shows another button
trans=Label(root,text='Random Label').pack()
backButton=Button(root,text="Back").pack()
againBtn=Button(root,text='Again').pack()
#transColorOn() #this just turns on an led
print('Turned on an LED')
translate()    #this is the translate function 
root=Tk()
base_frame=Frame(root)
base_frame.pack(fill='both')
lable=Label(base_frame,text='Hello World').pack()
selected=IntVar()
entry=Entry(base_frame, textvariable=selected)
entry.pack(padx=50)
sub_button=Button(base_frame,text='Submit',command=lambda:choose(selected))
sub_button.pack()
root.mainloop()

我使用了一个框架来获得最初的小部件集,后来称为frame.pack_forget(),它暂时删除框架及其子小部件,然后将新的小部件集中在根上(你也可以在另一个框架上这样做(,无论你在哪里需要初始屏幕,你都可以调用frame.pack()。您可以以相同的方式将其与任何几何图形管理器一起使用。让我知道这是你的问题还是我解释错了。

最新更新