我已經為一個看似簡單而微不足道的錯誤發瘋了好幾個小時,那就是NameError: name 'name' is not defined. name 是 Main.py 檔案中的一個組合框。我想將它傳遞給 B.py 檔案,但奇怪的是我無法恢復它。總結一下代碼,不再贅述,我只使用對解決問題有用的代碼。
在main.py檔案myproject/main中,我有:
from folder.B import example
name=ttk.Combobox(name_labelframe, width = 16)
name['value'] =["Element1", "Element2"]
name.place(x=6, y=5)
name.set("Select")
def function1():
print("Everything")
btn = Button(window, text="Click", command=function1)
btn.place(x=10, y=80)
在B.py子檔案中,我有:
def example(name):
if name.get():
print("good")
example(name)
我不知道這是否是重要資訊,但 B.py 檔案位于myproject/folder/B
我將“名稱”傳遞給函式并將檔案匯入主檔案。我不明白為什么它不起作用。有什么我忘了寫的嗎?你能幫助我嗎?謝謝
uj5u.com熱心網友回復:
在 B.py. 注釋掉#example(name)。洗掉按鈕小部件。我洗掉了import folder.
嘗試這個:
import tkinter as tk
from tkinter import ttk
from B import example
root = tk.Tk()
root.title('Mike_96a')
x = 'Mike_96a'
print(x)
_name = ttk.Combobox(root, values=[x])
_name.grid(column=0, row=1)
_name.current(0)
x= _name.get()
print(example(x))
root.mainloop()
在 B.py 模塊中:
def example(name):
if name:
print(f'Good morning {name}')
#example('name')
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/519426.html
標籤:Pythonpython-3.xtkinter进口模块
上一篇:遍歷df的所有值以替換零值
下一篇:DI容器中包含資料訪問層的問題
