如何通过按下按钮kivy/kivymd/python在另一个窗口中创建按钮
我正试图通过按按钮来创建按钮,但找不到有关它的任何信息。试试这个如何通过按下按钮kivy/kivymd/python在另一个窗口中创建按钮,python,button,kivy,kivy-language,kivymd,Python,Button,Kivy,Kivy Language,Kivymd,我正试图通过按按钮来创建按钮,但找不到有关它的任何信息。试试这个 from tkinter import * import tkinter as tk class MainWindow(tk.Frame): counter = 0 def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) self.button = tk.Button(self, t
from tkinter import *
import tkinter as tk
class MainWindow(tk.Frame):
counter = 0
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.button = tk.Button(self, text="Create new hotlink",
command=self.create_window)
self.button.pack(side="top")
def create_window(self):
self.counter += 1
t = tk.Toplevel(self)
t.wm_title("All the best to hackathon")
fields = 'Hotlink Name', 'URL'
def fetch(entries):
for entry in entries:
field = entry[0]
text = entry[1].get()
print('%s: "%s"' % (field, text))
def makeform(root, fields):
entries = []
for field in fields:
row = Frame(root)
lab = Label(row, width=15, text=field, anchor='w')
ent = Entry(row)
row.pack(side=TOP, fill=X, padx=5, pady=5)
lab.pack(side=LEFT)
ent.pack(side=RIGHT, expand=YES, fill=X)
entries.append((field, ent))
return entries
def button2():
newButton = tk.Button(root, text=fields[0])
newButton.pack(side=RIGHT)
ents = makeform(t, fields)
t.bind('<Return>', (lambda event, e=ents: fetch(e)))
b2 = Button(t, text='Save', command=button2)
b2.pack(side=LEFT, padx=5, pady=5)
if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
从tkinter导入*
将tkinter作为tk导入
类主窗口(tk.Frame):
计数器=0
定义初始化(self,*args,**kwargs):
tk.Frame.\uuuu init\uuuuu(self,*args,**kwargs)
self.button=tk.button(self,text=“创建新热链接”,
command=self.create\u窗口)
自助式按钮包(side=“top”)
def创建_窗口(自):
self.counter+=1
t=tk.顶层(自)
t、 wm_标题(“祝hackathon一切顺利”)
字段='热链接名称','URL'
def fetch(条目):
对于条目中的条目:
字段=条目[0]
text=条目[1]。获取()
打印(“%s:“%s”%”(字段,文本))
def makeform(根,字段):
条目=[]
对于字段中的字段:
行=帧(根)
lab=标签(行,宽度=15,文本=字段,锚点=w')
ent=条目(行)
行包装(侧面=顶部,填充=X,padx=5,pady=5)
实验室包装(侧面=左侧)
ent.pack(侧面=右侧,展开=是,填充=X)
条目。追加((字段,ent))
返回条目
def按钮2()
newButton=tk.Button(根,文本=字段[0])
纽扣包装(侧面=右侧)
ents=生成形式(t,字段)
t、 绑定(“”,(lambda事件,e=ents:fetch(e)))
b2=按钮(t,text='Save',command=buttonn2)
b2.组件(侧面=左侧,padx=5,pady=5)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
main=主窗口(根)
main.pack(side=“top”,fill=“both”,expand=True)
root.mainloop()
您可以在kivy或kivyMD中使用这种方法
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.button import MDRaisedButton
from kivy.properties import ObjectProperty
KV = '''
ScreenManager:
id: screen_manager
Screen:
id: screenID
MDRaisedButton:
text: "Create Button"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: app.createButton()
'''
class Example(MDApp):
screen_manager = ObjectProperty() # IMPORTANT!
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
def build(self):
return self.screen
def createButton(self):
print("button created")
self.btn = MDRaisedButton(text= "New Button", pos_hint= {'center_x': .5, 'center_y': .7})
self.root.ids.screenID.add_widget(self.btn)
Example().run()
谢谢你,但我正在寻找奇维代码