目標是允許用戶從組合框下拉選擇,然后點擊選擇檔案按鈕。該按鈕將獲得產品型別(來自組合框的值),并將根據組合框中選擇的內容打開正確的目錄。問題是,我似乎無法得到這個值,因為按鈕和組合框在不同的框架中。我覺得我在這里錯過了一些基本的東西。
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
def select_files(prod_type)。
path = f"10.10.3.7Production/Test_Folder{prod_type}"。
filetypes = (
('PDF Files', '*.pdf') 。
)
filenames = fd.askopenfilenames(
title='Open files',
initialdir=path,
filetypes=filetypes)
for file in filenames:
print(file)
class InputFrame(ttk.Frame)。
def __init__(self, parent)。
super().__init__(parent)
# 設定網格布局管理器。
self.columnconfigure(0, weight=1)
self.columnconfigure(0, weight=3)
self.__create_widgets()
def __create_widgets(self):
# Product[/span
ttk.Label(self, text='Product:').grid( column=0, row=0, sticky=tk.W)
self.product_type = tk.StringVar()
self.product_combo = ttk.Combobox(self, width=30, textvariable=self.product_type)
自己。 product_combo['values'] = ('記事本', '扁平記事本', '折疊式記事本', '期刊')
self.product_combo.set('notepad')
self.product_combo['state'] = 'readonly'。
self.product_combo.grid( column=1, row=0, sticky=tk.W)
# 演算法:
ttk.Label(self, text='Algorithm:').grid( column=0, row=1, sticky=tk.W)
algo_var = tk.StringVar()
algo_combo = ttk.Combobox(self, width=30)
algo_combo[' values'] = ('panel', 'fuzzy')
algo_combo.set('panel')
algo_combo.grid( column=1, row=1, sticky=tk.W)
# Orientation:'Orientation:').grid( column=0, row=2, sticky=tk.W)
orientation_var = tk.StringVar()
orientation_combo = ttk.Combobox(self, width=30, textvariable=orientation_var)
orientation_combo[' values'] = ('auto', 'portrait', 'landscape')
orientation_combo.set('auto')
orientation_combo.grid( column=1, row=2, sticky=tk.W)
# margin:
ttk.Label(self, text='Margin:').grid(column=0, row=3, sticky=tk.W)
margin = ttk.Entry(self, width=30)
margin.grid( column=1, row=3, sticky=tk.W)
# gap:
ttk.Label(self, text='Gap:').grid(column=0, row=4, sticky=tk.W)
gap = ttk.Entry(self, width=30)
gap.grid( column=1, row=4, sticky=tk.W)
# 重復:
ttk.Label(self, text='Repeat:').grid( column=0, row=5, sticky=tk.W)
repeat_number = tk.StringVar()
repeat = ttk.Spinbox(self, from_=1, to=10, width=30, textvariable=repeat_number)
repeat.set(1)
repeat.grid( column=1, row=5, sticky=tk.W)
for widget in self.winfo_children()。
widget.grid(padx=0, pady=5)
class ButtonFrame(ttk.Frame)。
def __init__(self, parent)。
super().__init__(parent)
# 設定網格布局管理器。
self.columnconfigure(0, weight=1)
self.__create_widgets()
def __create_widgets(self):
ttk.Button(self, text='Select Files', command=self.on_go_pressed).grid( column=0, row=0)
ttk.Button(self, text='Generate PDF').grid( column=0, row=1)
for widget in self.winfo_children() 。
widget.grid(padx=0, pady=3)
def on_go_pressed(self)。
select_files(InputFrame.product_type.get())
class App(tk.Tk)。
def __init__(self):
super().__init__()
self.title("PDF n-up")
self.eval('tk::PlaceWindow . center')
self.geometry('400x200')
self.resizable(0, 0)
self.attribute('-toolwindow', True)
#布局在根視窗上。
self.columnconfigure(0, weight=4)
self.columnconfigure(1,weight=1)
self.__create_widgets()
def __create_widgets(self):
# 創建輸入框。
input_frame = InputFrame(self)
input_frame.grid( column=0, row=0)
#創建按鈕框架。
button_frame = ButtonFrame(self)
button_frame.grid( column=1, row=0)
if __name__ == '__main__':
app = App()
app.mainloop()
uj5u.com熱心網友回復:
你可以在創建ButtonFrame時將產品型別變數傳遞給它,因為你在創建InputFrame后創建了ButtonFrame。以下是代碼,并相應地標出了修改和添加的行數:
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
def select_files(prod_type)。
path = f"/home/sam/Pictures/Test/{prod_type}"
filetypes = (
('PDF Files', '*.pdf') 。
)
filenames = fd.askopenfilenames(
title='Open files',
initialdir=path,
filetypes=filetypes)
for file in filenames:
print(file)
class InputFrame(ttk.Frame)。
def __init__(self, parent)。
super().__init__(parent)
# 設定網格布局管理器。
self.columnconfigure(0, weight=1)
self.columnconfigure(0, weight=3)
self.__create_widgets()
def __create_widgets(self):
# Product[/span
ttk.Label(self, text='Product:').grid( column=0, row=0, sticky=tk.W)
self.product_type = tk.StringVar()
self.product_combo = ttk.Combobox(self, width=30, textvariable=self.product_type)
自己。 product_combo['values'] = ('記事本', '扁平記事本', '折疊式記事本', '期刊')
self.product_combo.set('notepad')
self.product_combo['state'] = 'readonly'。
self.product_combo.grid( column=1, row=0, sticky=tk.W)
# 演算法:
ttk.Label(self, text='Algorithm:').grid( column=0, row=1, sticky=tk.W)
algo_var = tk.StringVar()
algo_combo = ttk.Combobox(self, width=30)
algo_combo[' values'] = ('panel', 'fuzzy')
algo_combo.set('panel')
algo_combo.grid( column=1, row=1, sticky=tk.W)
# Orientation:'Orientation:').grid( column=0, row=2, sticky=tk.W)
orientation_var = tk.StringVar()
orientation_combo = ttk.Combobox(self, width=30, textvariable=orientation_var)
orientation_combo[' values'] = ('auto', 'portrait', 'landscape')
orientation_combo.set('auto')
orientation_combo.grid( column=1, row=2, sticky=tk.W)
# margin:
ttk.Label(self, text='Margin:').grid(column=0, row=3, sticky=tk.W)
margin = ttk.Entry(self, width=30)
margin.grid( column=1, row=3, sticky=tk.W)
# gap:
ttk.Label(self, text='Gap:').grid(column=0, row=4, sticky=tk.W)
gap = ttk.Entry(self, width=30)
gap.grid( column=1, row=4, sticky=tk.W)
# 重復:
ttk.Label(self, text='Repeat:').grid( column=0, row=5, sticky=tk.W)
repeat_number = tk.StringVar()
repeat = ttk.Spinbox(self, from_=1, to=10, width=30, textvariable=repeat_number)
repeat.set(1)
repeat.grid( column=1, row=5, sticky=tk.W)
for widget in self.winfo_children()。
widget.grid(padx=0, pady=5)
class ButtonFrame(ttk.Frame)。
def __init__(self, parent, product_type)。### EDITED THIS LINE.
super().__init__(parent)
# setting the grid layout manager
self.columnconfigure(0, weight=1)
self.product_type = product_type ### ADDED THIS LINE[/span]。
self.__create_widgets()
def __create_widgets(self):
ttk.Button(self, text='Select Files', command=self.on_go_pressed).grid( column=0, row=0)
ttk.Button(self, text='Generate PDF').grid( column=0, row=1)
for widget in self.winfo_children() 。
widget.grid(padx=0, pady=3)
def on_go_pressed(self)。
select_files(self.product_type.get()) ### edited this line: select_files(self.product_type.get())
class App(tk.Tk)。
def __init__(self):
super().__init__()
self.title("PDF n-up")
self.eval('tk::PlaceWindow . center')
self.geometry('400x200')
self.resizable(0, 0)
# self.attributions('-toolwindow', True)。
# layout on the root window
self.columnconfigure(0, weight=4)
self.columnconfigure(1,weight=1)
self.__create_widgets()
def __create_widgets(self):
# 創建輸入框。
input_frame = InputFrame(self)
input_frame.grid( column=0, row=0)
#創建按鈕框架。
button_frame = ButtonFrame(self, input_frame.product_type) ### EDITED THIS LINE[/span].
button_frame.grid( column=1, row=0)
if __name__ == '__main__':
app = App()
app.mainloop()
注意到在button_frame = ButtonFrame(self, input_frame.product_type)這一行,我給了它input_frame.product_type一個引數,所以它可以在任何時候訪問它。
為此需要對ButtonFrame進行修改。我改變了__init__()方法,這樣它就可以接收另一個引數。然后,它將product_type引數的值分配給一個變數,以便以后可以使用它。當它呼叫select_files()時,它只需使用self.product_type.
至于為什么呼叫select_files(InputFrame.product_type.get())不起作用。product_type是一個變數,只在InputFrame被初始化時創建。在上面這一行中,你參考了類本身,而不是類的實體,所以它還沒有被初始化。因此,在 InputFrame.__init__() 中創建的任何東西都沒有被定義,因為 __init__() 沒有被呼叫。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/319448.html
標籤:
