我有一個供教師使用的 Python 腳本,它在我的終端上運行良好,我正在嘗試使用 Tkinter,因此我可以與其他人共享此腳本。在腳本中,用戶將檔案和其他資訊上傳到 SQLite 資料庫。
下面是腳本的一部分,僅顯示第一個選項卡(共 3 個)的內容。問題在于,當人們與資料庫互動時,選項卡需要重繪 以顯示發生了一些事情并且資料庫中的檔案串列已更改。
我讀過的所有內容都表明您需要一個按鈕來重繪 。但這不是用戶友好的。人們想要做的是上傳一個檔案,然后在串列中看到該檔案。這對 Tkinter 是否可行,如果可以,如何實作?
class AppWindow():
my_list = [4]
def __init__(self, parent):
global my_list
# Create the window
self.window = parent
self.window.geometry("700x600")
#self.center_window()
self.window.title("Test app")
# Create a text label and place it in the window
self.hello_label = tk.Label(self.window, text="Hello world!")
self.hello_label.place(x=20, y=20)
# Create 3 tabs
self.tab_container = tk.Frame(self.window)
self.tab_container.place(x=0,y=0,width=700,height=400)
self.tabs = ttk.Notebook(self.tab_container)
self.tab_2 = tk.Frame(self.tabs)
self.tabs.add(self.tab_2, text="Parameters")
self.tabs.place(x=0,y=0,height=400,width=700)
# Content for tab 2
self.label2 = tk.Label(self.tab_2, text="")
self.label201=tk.Label(self.tab_2, text="Put in your target GPA")
self.label201.place(x=50, y=50)
btn1 = tk.Button(self.tab_2,text="Target GPA", command=self.getGPA)
btn1.place(x=50, y=80)
for lst in self.my_list:
btn99=tk.Button(self.tab_2,text=lst)
btn99.grid()
def getGPA(self):
userInput = sd.askstring('User Input','Enter target GPA')
self.my_list.append(userInput)
if __name__ == "__main__":
root = tk.Tk()
app = AppWindow(root)
root.mainloop()
uj5u.com熱心網友回復:
這只是一個猜測,因為我已經在您的問題下的評論中提到了所有原因。一般來說,要更新或您所謂的“重繪 ”選項卡,需要添加、更改或洗掉您放置在其上的小部件。
下面的代碼基于您代碼中的當前內容。每次Target GP單擊該按鈕時,都會將另一個按鈕Button添加到self.tab_2框架中并附加到my_list. 更改小部件不必通過單擊按鈕觸發,它可能是某些其他事件的結果(例如在后臺完成檔案上傳)。
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.simpledialog as sd
class AppWindow():
def __init__(self, parent):
# Create the window
self.window = parent
self.window.geometry("700x600")
#self.center_window()
self.window.title("Test app")
# Create and initialize data list.
self.my_list = [4]
# Create a text label and place it in the window
self.hello_label = tk.Label(self.window, text="Hello world!")
self.hello_label.place(x=20, y=20)
# Create 3 tabs
self.tab_container = tk.Frame(self.window)
self.tab_container.place(x=0, y=0, width=700, height=400)
self.tabs = ttk.Notebook(self.tab_container)
self.tab_2 = tk.Frame(self.tabs)
self.tabs.add(self.tab_2, text="Parameters")
self.tabs.place(x=0, y=0, height=400, width=700)
# Content for tab 2
self.label201 = tk.Label(self.tab_2, text="Put in your target GPA")
self.label201.place(x=50, y=50)
btn1 = tk.Button(self.tab_2, text="Target GPA", command=self.getGPA)
btn1.place(x=50, y=80)
# Create a Button for each item currently in list.
for item in self.my_list:
btn = tk.Button(self.tab_2, text=item)
btn.grid()
def getGPA(self):
userInput = sd.askstring('User Input', 'Enter target GPA')
if userInput is None: # User closed the dialog or clicked Cancel?
return
self.my_list.append(userInput)
btn = tk.Button(self.tab_2, text=userInput) # Create another Button.
btn.grid()
if __name__ == "__main__":
root = tk.Tk()
app = AppWindow(root)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375693.html
