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

2024-10-05 15:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试着做一个基本莫尔斯电码翻译器。我希望它首先显示一个窗口,让您可以选择不同的模式。我希望能够选择翻译模式,然后将窗口更改为“翻译屏幕”,然后运行翻译功能

相反,当我选择我的模式时,它会运行该功能,然后一旦该功能完成,它就会切换到“翻译屏幕”

我尝试过改变事情运行的顺序,但没有任何效果。我不知道该怎么办。我也没有用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 

这是translate函数

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 done\n")
    while tempIN != "done":
        if tempIN == ".":
            LetterToSend = "" + LetterToSend + "."
            print("" + (LetterToSend))
            tempIN = input(". or - , then done\n")
        elif tempIN == "-":
            LetterToSend = "" + LetterToSend + "-"
            print("" + (LetterToSend))
            tempIN = input(". or - , then done\n")
        else:
            print("plsss valid input\n")
            tempIN = input(". or - , then done\n")

帮助 谢谢


Tags: or功能inputifdef模式columnthis
1条回答
网友
1楼 · 发布于 2024-10-05 15:29:16

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

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 done\n")
    '''while tempIN != "done":
        if tempIN == ".":
            LetterToSend = "" + LetterToSend + "."
            print("" + (LetterToSend))
            tempIN = input(". or - , then done\n")
        elif tempIN == "-":
            LetterToSend = "" + LetterToSend + "-"
            print("" + (LetterToSend))
            tempIN = input(". or - , then done\n")
        else:
            print("plsss valid input\n")
            tempIN = input(". or - , then done\n")'''
    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()。可以以相同的方式将其与任何几何图形管理器一起使用。让我知道这是你的问题还是我解释错了

相关问题 更多 >