簡體   English   中英

如何在 B 類(python)中使用 A 類的變量

[英]how can I use the variable from class A in class B (python)

我想在類(PageOne)中使用變量(文件)我不擅長python,所以即使我做了研究我也做不到,你能幫幫我嗎? 我的項目是從用戶(在 WelcomePage 中)獲取一個 csv 文件,它將從該文件中選擇一個隨機行並將其顯示在(PageOne)中。

我嘗試了不同的方法,但總是相同的錯誤消息(名稱“文件”未定義)

from cProfile import label
import tkinter as tk
from turtle import title
from typing import Container
from tkinter import font as tkfont
from tkinter.filedialog import askopenfile 


class MainFrame (tk.Tk):




   def __init__(self,*args,**kwargs):
       tk.Tk.__init__(self,*args,**kwargs)

       self.titlefont = tkfont.Font(family = 'Verdana',size=12,
                                   weight = "bold", slant='roman')

       container =tk.Frame()
       container.grid(row=0, column=0, sticky='nesw')

       self.id =tk.StringVar()
       self.id.set("Mister")

       self.listing = {}

       for p in (WelcomePage, PageOne):
           page_name = p.__name__
           frame = p(parent = container, controller = self)
           frame.grid(row=0, column=0, sticky='nesw')
           self.listing[page_name] = frame

       self.up_frame('WelcomePage')

   def up_frame(self,page_name):
       page = self.listing[page_name]
       page.tkraise()

class WelcomePage(tk.Frame):
   global file
   def open_file():
       
       file = askopenfile(mode='r', filetypes=[('I', '*csv')])
       if file is not None:
           pass
       return file

   def __init__(self, parent, controller):
       tk.Frame.__init__(self, parent)
       self.controller = controller
       self.id = controller.id

       label = tk.Label(self,text= "Welcome Page \n"+ controller.id.get(),
                        font= controller.titlefont)
       label.pack()
       lod = tk.Button (self, text="Select a file",
                        command= lambda:open_file())
       lod.pack()
       bou = tk.Button (self, text="Submit",
                        command= lambda: controller.up_frame("PageOne"))
       bou.pack()
       
   






class PageOne(WelcomePage,tk.Frame):
   def __init__(self, parent, controller):
       tk.Frame.__init__(self, parent)
       self.controller = controller
       self.id = controller.id

       label = tk.Label(self,text= "Page One \n"+ controller.id.get(),
                        font= controller.titlefont)
       label.pack()

       bou = tk.Button (self, text="back to main",
                        command= lambda: controller.up_frame("WelcomePage"))
       bou.pack()
       
       
       

       

if __name__ == '__main__':
   app = MainFrame()
   app.mainloop()

試試這個——在方法中使用關鍵字 global 以及用於變量目的

那么您的文件名錯誤未定義應該解決。

   def open_file():
   global file
   file = askopenfile(mode='r', filetypes=[('I', '*csv')])
   if file is not None:
       pass
   return file

在 Python 3.8 中使用海象。 把關鍵字放在函數里面

之前

   def open_file():       
       file = askopenfile(mode='r', filetypes=[('I', '*csv')])
       if file is not None:
           pass
       return file

海象之后

def open_file():
    global file
    if (file := askopenfile(mode='r', filetypes=[('I', '*py' is not None)])):
       pass
    return file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM