第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自定義 python 模塊中的變量

自定義 python 模塊中的變量

qq_遁去的一_1 2022-01-18 17:50:03
我正在制作我自己的名為 zoro 的自定義 python 模塊,并且我想讓人們制作一個變量,并且該變量等于我模塊中的一個函數(shù),但我該怎么做呢?我已經(jīng)嘗試查看其他模塊(如turtle)的代碼,turtle 使用了 self 參數(shù),所以我嘗試使用它,但它說TypeError: win() missing 1 required positional argument: 'self'.我的程序代碼來測試模塊:import zorotest = zoro.win("test","black",500,500)test.zoro.winTitle("test2")我的模塊代碼:from tkinter import *def win(title,bg,w,h):    root = Tk()    root.title(title)    root.config(bg=bg)    root.geometry(str(w) + "x" + str(h))    return rootdef winTitle(title):    root.title(title)我想做這樣的事情:test = zoro.win("test","black",500,500)test.zoro.winTitle("test2")
查看完整描述

2 回答

?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗 獲得超5個贊

問題:


你想做的事情叫做inheritance. 例如:


佐羅.py


import tkinter as tk


class App(tk.Tk):

    def __init__(self, title, bg, width, height):

        super().__init__()

        self.title(title)

        self.geometry('{}x{}'format(width, height)

        self.config(bg=bg)

用法


import zoro


class MyApp(zoro.App):

    def __init__(self):

        super().__init__("test","black",500,500)


        # Change title

        self.title('my new title')


        # Add further widgets


if __name__ == '__main__':

    MyApp().mainloop()


查看完整回答
反對 回復(fù) 2022-01-18
?
萬千封印

TA貢獻(xiàn)1891條經(jīng)驗 獲得超3個贊

假設(shè)您希望您的驅(qū)動程序使用當(dāng)前定義的模塊,您需要一個名為rootfor的全局變量winTitle來使用。此外,返回的對象win沒有名為 的屬性zoro。


import zoro


zoro.root = zoro.win("test", "black", 500, 500)

zoro.winTitle("test2")

也就是說,您的模塊應(yīng)該首先被修復(fù)以避免全局變量。


from tkinter import *



def win(title, bg, w, h):

    root = Tk()

    root.title(title)

    root.config(bg=bg)

    root.geometry(str(w) + "x" + str(h))

    return root



def winTitle(root, title):

    root.title(title)

然后你的司機看起來像


import zoro


test = zoro.win("test", "black", 500, 500)

zoro.winTitle(test, "test2")


查看完整回答
反對 回復(fù) 2022-01-18
  • 2 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號