我想得到什么?如果 second.py 檔案的條件為真,則 main.py 檔案的文本框中將列印“ok”。如果條件為 False,則“否”將列印在同一文本框中。
main.py 是由 Home.py 檔案打開的檔案。所以最初有一個 Home.py 檔案,它在其中打開 main.py 檔案。main.py 檔案執行 second.py 檔案。我只發布了主檔案和第二個檔案,因為主檔案在主檔案中正確打開
Home.py > main.py > second.py
在 main.py 檔案中,我有一個文本框和一個按鈕。準確地說,它們都在主檔案的一個類中,但在代碼中我只攜帶了示例所需的最低限度。通過單擊按鈕,我想匯入位于另一個檔案的另一個類中的代碼( second.py 檔案)。secondary.py 檔案的代碼用于生成要在 main.py 檔案的文本框中列印的文本(帶條件)。這是一個簡單的案例,但有問題。
錯誤是:
TypeError: __init__() missing 1 required positional argument: 'master'
主檔案
from tkinter import *
from tkinter import ttk
import tkinter as tk
class Home(tk.Frame): #update
def __init__(self, master): #update
super().__init__(master, **kw) #update
#class import from other file
from Folder.Folder2 import second
k = second.Print
#i want to print the output here
self.textobox = tk.Text(master, width=33, height=10, font=('helvetic', 12))
self.textobox.place(x=30, y=40)
self.button = Button(master, text="Ok", command= lambda: [one_function(), two_function()])
self.button.pack()
第二個.py
import sqlite3
class Print:
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.conn = sqlite3.connect('...')
self.cursor = conn.cursor()
self.cursor.execute('SELECT x, y FROM Table')
self.element = cursor.fetchone()
self.cursor.execute("SELECT day FROM quantity")
self.day = cursor.fetchone()
#condition to print text in the textbox of the main.py file
if self.day == "Monday" and (self.element[0]) >= (self.element[1]):
textobox.insert(tk.END, "ok")
else:
textobox.insert(tk.END, "no")
資料庫非常簡單:
CREATE TABLE "Table" (
"id" INTEGER,
"x" INTEGER, #for example 23
"y" INTEGER, #for example 12
PRIMARY KEY("id")
);
CREATE TABLE "quantity" (
"id" INTEGER,
"day" INTEGER, #for example Monday
PRIMARY KEY("id")
);
uj5u.com熱心網友回復:
有很多錯別字second.py:
conn.cursor()應該self.conn.cursor()cursor.execute(...)應該self.cursor.execute(...)
除了錯別字:
super().__init__(...)不應該被稱為Table是 SQLite3 中的關鍵字,所以如果用作表名,則需要將其放在引號內,例如"Table". 建議改用其他名稱。textobox未定義(它是Home類中的實體變數main.pytk.END未定義master并且**kw引數不是必需的,因為它沒有被使用self.day是一個元組(如果回傳記錄)或None
也建議改為self.textobox在Home課堂上傳遞Print。
下面是修改后的second.py:
import sqlite3
class Print:
def __init__(self, textbox): # added textbox argument
self.conn = sqlite3.connect('...')
self.cursor = self.conn.cursor()
self.cursor.execute('SELECT x, y FROM "Table"')
self.element = self.cursor.fetchone()
self.cursor.execute("SELECT day FROM quantity")
self.day = self.cursor.fetchone()
#condition to print text in the textbox of the main.py file
if (self.day and self.day[0] == "Monday") and (self.element and self.element[0] >= self.element[1]):
textbox.insert("end", "ok")
else:
textbox.insert("end", "no")
對于main.py:
super().__init__()也不應該被呼叫k = second.Print應該是k = second.Print(...)并且應該在self.textobox()創建后呼叫one_function()并且two_function()是未定義的。(它們是否在其他地方定義?)
下面是修改后的main.py:
import tkinter as tk
from Folder.Folder2 import second
class Home:
def __init__(self, master):
#i want to print the output here
self.textobox = tk.Text(master, width=33, height=10, font=('helvetic', 12))
self.textobox.place(x=30, y=40)
self.button = tk.Button(master, text="Ok", command= lambda: [one_function(), two_function()])
self.button.pack()
k = second.Print(self.textobox) # pass self.textobox to Print class
main = tk.Tk()
main.geometry("400x300")
my_gui = Home(main)
main.mainloop()
請注意,按以下方式匯入模塊是不好的做法:
from tkinter import *
import tkinter as tk
因為不建議使用通配符匯入,并且tkinter模塊已匯入兩次。建議使用第二次匯入。
根據相關更改更新了 main.py :
import tkinter as tk
from Folder.Folder2 import second
class Home(tk.Frame):
def __init__(self, master, **kw):
super().__init__(master, **kw)
#i want to print the output here
self.textobox = tk.Text(self, width=33, height=10, font=('helvetic', 12))
self.textobox.place(x=30, y=40)
self.button = tk.Button(self, text="Ok", command= lambda: [one_function(), two_function()])
self.button.pack()
k = second.Print(self.textobox)
main = tk.Tk()
main.geometry("400x300")
my_gui = Home(main)
my_gui.pack(fill="both", expand=1)
main.mainloop()
請注意,我在創建按鈕和文本框時使用self了而不是。master
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411583.html
標籤:
