Tkinter首先打开第二个GUI

时间:2019-10-03 12:42:28

标签: python tkinter

我正在尝试建立一个登录屏幕进行练习,但遇到了麻烦。通常的想法是,按下“登录”按钮将关闭当前窗口,并在单独的文件中打开另一个窗口。但是,当我运行主文件时,它将打开在第二个文件中创建的GUI窗口。我不确定会导致它执行此操作的原因。

BluetoothAdapter mBluetoothAdapter;

// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
        }
    }
};

public void TurnOnBluetooth(){
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); //Init bluetoothadapter

}
public void enableDisableBT(){
    if(mBluetoothAdapter == null){
        Log.d(TAG, "enableDisableBT: Does not have BT capabilities.");
    }
    if (!mBluetoothAdapter.isEnabled()){
        Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableBTIntent);
        IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBroadcastReceiver1, BTIntent);
    }
    if (mBluetoothAdapter.isEnabled()){
        mBluetoothAdapter.disable();
        IntentFilter BTIntent = new Intent tFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBroadcastReceiver1, BTIntent);
    }
}

然后是第二个文件

import tkinter as tk
import loginEntry

HEIGHT = 200
WIDTH = 500


def login_function():
    root.destroy()
    loginEntry.NewScreen()


def register_function():
    print("Register!")


root = tk.Tk()

root.title("Login Screen")
root.resizable(False, False)

canvas = tk.Canvas(root, height = HEIGHT, width = WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='grey')
frame.place(relx=0.1, rely=0.25, relwidth=0.8, relheight=0.5)

login = tk.Button(frame, text="Login", command=login_function)
login.place(relx=0.05, rely=0.25, relwidth=0.425, relheight=0.5,)

register = tk.Button(frame, text="Register", command=register_function)
register.place(relx=0.525, rely=0.25, relwidth=0.425, relheight=0.5,)

introduction = tk.Label(root, text="Hello and welcome to DogNet, please login below.", font='bold 12')
introduction.place(relx=0.5, anchor='center', rely=0.1)

root.mainloop()

1 个答案:

答案 0 :(得分:1)

这是因为在导入第二个文件时,您正在调用定义。

import tkinter as tk

def NewScreen():
    root = tk.Tk()

    canvas = tk.Canvas(root, bg='black')
    canvas.pack()

    root.mainloop()

NewScreen() #< here you call the definition

这意味着程序中发生的一系列事件如下。

  1. 第一个文件开始
  2. 第二个文件已导入
  3. 定义“ NewScreen”被称为
  4. tkinter mainloop会启动,这会将您的程序置于循环中,并阻止程序继续进行任何新行,直到该循环关闭为止