简体   繁体   English

Tkinter 将数据从数据库显示到新 window

[英]Tkinter displaying data from database to new window

I'm making a management system with a function to view the database.我正在制作一个带有 function 的管理系统来查看数据库。 I want to make it where a new window will pop up displaying the database when the user clicks the "view list" button.我想在用户单击“查看列表”按钮时弹出一个新的 window 以显示数据库的位置。 I'm saving the "list" function as a new.py file and i'm calling it using the code below in the main file我将“列表”function 保存为 new.py 文件,我在主文件中使用以下代码调用它

def listCustomer():
call(["python", "list.py"])

However, I can't seem to make the data appear on the new window as everytime the new window appears its an empty window.但是,我似乎无法让数据出现在新的 window 上,因为每次新的 window 出现它是一个空的 window。 Can anyone tell me what I did wrong?谁能告诉我我做错了什么? I'll attach my code for the list.py file below我将在下面附上我的 list.py 文件的代码

from tkinter import *
from PIL import ImageTk,Image
import sqlite3

root = Tk()


def list():
    #Create database
    connect = sqlite3.connect("hmsdata.db")
    #Create cursor
    c = connect.cursor()

    #List database
    c.execute("SELECT *, oid FROM details")
    data = c.fetchall()
    showData=''
    for data in details:
        showData += str(data)+"\n"

    dataLabel = Label(root, text=showData)
    dataLabel.grid(row=0, column=0)

    #Commit changes
    connect.commit()
    #Close connection
    connect.close()

This is what happens when I click the "View Customer" Button, it does not display anything这就是我单击“查看客户”按钮时发生的情况,它不显示任何内容

You could use normal way - import list - and later execute list.list() .您可以使用正常方式 - import list - 稍后执行list.list() This way you should have better control on windows.这样您应该可以更好地控制 windows。

But it needs to use Toplevel to create second window.但它需要使用Toplevel创建第二个 window。

Minimal working example.最小的工作示例。

main.py主文件

import tkinter as tk  # PEP8: `import *` is not preferred
import list as ls     # `list` is use to create list - don't use this name

# --- functions ---

def on_click():
    # run method `list()` from file `list`
    ls.list()

# --- main ---

root = tk.Tk()

button = tk.Button(root, text='Second', command=on_click)
button.pack()

root.mainloop()

list.py列表.py

import tkinter as tk  # PEP8: `import *` is not preferred

def list():
    second = tk.Toplevel()  # create inside function

    dataLabel = tk.Label(second, text="Hello World")
    dataLabel.grid(row=0, column=0)

BTW: In your version you probably forgot to execute list()顺便说一句:在您的版本中,您可能忘记执行list()


EDIT:编辑:

Version which check if window is not open already ( main.py is the same)检查 window 是否尚未打开的版本( main.py相同)

list.py列表.py

import tkinter as tk  # PEP8: `import *` is not preferred

second = None  # to control if window is open

def on_close():
    global second
    
    second.destroy()
    second = None  # to control if window is open
    
def list():
    global second
    
    if second: # if second is not None:
        print('Window already open')
    else:
        second = tk.Toplevel()

        dataLabel = tk.Label(second, text="Hello World")
        dataLabel.grid(row=0, column=0)

        button = tk.Button(second, text='Close', command=on_close)
        button.grid(row=1, column=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM