在Tkinter中使用顶层在另一个模块的窗口上创建新窗口

时间:2020-10-30 20:40:33

标签: python tkinter module toplevel

我想使用顶层方法在另一个模块的窗口上方创建一个窗口。

如果不使用面向对象的方法,是否可以将GUI分为模块?

这是一个简单的示例,第一个模块(file1.py):

import tkinter
from file2 import user

global MainWindow
MainWindow = tkinter.Tk()
MainWindow.geometry("320x300")
MainWindow.title("")

def check():   
    user()

button1 = tkinter.Button(MainWindow,width = '15'
                         ,text="check", bg="green", fg="orange"
                         ,command = check)
button1.place(x=20,y=20)

MainWindow.mainloop()

,另一个模块是(file2.py):

from tkinter import *
from file1 import MainWindow

def user():
    userWindow = Toplevel(MainWindow)
    userWindow.geometry("320x300")
    userWindow.title("hello")

    user_name = Label(userWindow, text = "hello")
    user_name.place(x = 20, y = 190)

,但是在这种情况下是不可能的。我想知道为什么不可能,是否 我正在寻找解决方案。

1 个答案:

答案 0 :(得分:0)

您没有提到为什么您认为这是不可能的,尽管我猜测是因为import { createAWSConnection, awsGetCredentials, } from '@acuris/aws-es-connection'; import { Client } from '@elastic/elasticsearch'; export const getESClient = async () => { const esEndpoint = process.env.AWS_ES_ENDPOINT; if (!esEndpoint) { throw new Error( 'AWS_ES_ENDPOINT ENV not set.' ); } const awsCredentials = await awsGetCredentials(); const AWSConnection = createAWSConnection(awsCredentials); const client = new Client({ ...AWSConnection, node: esEndpoint, }); return client; }; export const createNewIndex = async (index: string) => { try { const client = await getESClient(); const exists = await client.indices.exists({ index }); if (!exists || !exists.statusCode || exists.statusCode !== 404) { console.log(`Index ${index} might alrady exist.`, exists); return false; } const created = await client.indices.create({ index, body: { mappings: { properties: { product_id: { type: 'keyword', }, product_description: { type: 'text', }, }, }, }, }); console.log(`Index created for ${index}`, created); } catch (error) { console.log(`Error creating index ${index}`, error); return false; } return true; }; 不知道user是什么。这样,您就可以进行循环导入。

一种简单的解决方案是将其作为参数传递,而不是MainWindow尝试导入file2.py

file1.py

# file1.py
...
def check():   
    user(MainWindow)
...