简体   繁体   English

如何在 tkinter 中使用按钮添加内容?

[英]How do I add things with button in tkinter?

So, what i wanna do is that when i click the button, a new thing to be added to my project.所以,我想做的是当我点击按钮时,一个新的东西被添加到我的项目中。

I have few tabs, and on the second one (WorkExp) I got Company and job description labels, and i want that whenever i click the button it to add new same labels.我有几个标签,在第二个标签(WorkExp)上,我得到了公司和职位描述标签,我希望每当我点击按钮时,它都会添加新的相同标签。

it works, the button, but the thing is add the placement on these new labels is the same as old ones.它可以工作,按钮,但问题是在这些新标签上添加的位置与旧标签相同。

I tried while and for cycle but i couldnt make any of them work.我尝试了 while 和 for 循环,但我无法让它们中的任何一个工作。

What I have tried:我试过的:

WorkExp = ttk.Frame(Tabs)
Tabs.add(WorkExp, text = "Work Experience")

######################
def AddExp():
Label(WorkExp, text = "Company/Place", padx = 5, pady = 5).grid(row = 3, column = 1)
Label(WorkExp, text="Job Description", padx=5, pady=5).grid(row = 4 , column=1)
Comp2 = Entry(WorkExp).grid(row=3, column=2)
Work2 = Entry(WorkExp).grid(row=4, column=2)

######################
Label(WorkExp, text = "Company/Place", padx = 5, pady = 5).grid(row = 1, column = 1)
Label(WorkExp, text = "Job Description", padx = 5, pady = 5).grid(row = 2, column = 1)

Comp1 = Entry(WorkExp).grid(row = 1, column = 2)
Work1 = Entry(WorkExp).grid(row = 2, column = 2)

Button(WorkExp, text = "Add Experience", command = AddExp).grid(row = 10, column = 1)
import Tkinter as tk

# Now Start From Here
class App(object):
    def new_row(self):
        # Create widgets -----
        new_entry = tk.Entry(root, width=7)

        # Put widgets in grid----------
        self.num_rows += 1
        new_entry.grid(column=0, row=self.num_rows, sticky='WE')

    def __init__(self):
        self.num_rows = 1
        createRow_button = tk.Button(
            root, text='New Row', command=self.new_row)
        createRow_button.grid()

root = tk.Tk()
app = App()
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM