如何使用Python 3.6

时间:2017-09-14 17:45:20

标签: python-3.x tkinter

我是这个网站的新手和编程。现在我面临挑战:我必须使用Python 3.6.2将表单中的条目插入到MySQL表中。有问题的窗口是动态的,这意味着我可以在其中添加或删除字段(显然,数据库中的表格不会相应地修改)。

我真正需要的是从表单中捕获一些条目(并省略重复的条目,如"确认密码"和"确认电子邮件"),但也来自地址2(这是可选的。)

下面我粘贴我的代码(其中一些改编自本网站)。我省略了SQL查询,因为我不知道应该放在哪里。我需要的数据来自" def create_window2()"。

提前致谢。

import tkinter as tk
import MySQLdb
from tkinter import *

class MainWindow(tk.Frame):
    counter = 0         
    def create_window2(self):
        t = tk.Toplevel(self)
        t.focus_force()
        t.wm_title("New Customer Registration")
        fields = ("Username*", "Password*", "Confirm password*", "First 
                  Name*", "Last Name*", "Address 1*", "Address 2 
                  (optional)", "Town/City", "Telephone Number*", "Email 
                  address*", "Confirm email*")

        def fetch(entries):
            for entry in entries:
                field = entry[0]
                text = entry[1].get()
                print('%s: "%s"' % (field, text))

        def makeform(t, fields):
            entries = []
            for field in fields:
                row = Frame(t)
                label = Label(row, width = 20, text = field, anchor = 'w')
                entry = Entry(row, width = 25)
                row.pack(side = TOP, padx = 5, pady = 5)
                label.pack(side = LEFT)
                entry.pack(side = LEFT)
                entries.append((field, entry))
            return entries

        if __name__ == '__main__':
            root = t
            root.geometry('+520+120')
            ents = makeform(root, fields)
            wm_button1 = Button(root, text = "Register", width = 15,
                                command = t.register)
            wm_button1.pack(side = LEFT, padx = 35, pady = 5)
            wm_button2 = Button(root, text = "Cancel", width = 15,
                                command = t.destroy)
            wm_button2.pack(side = LEFT, padx = 10, pady = 5)
            root.bind('<Return>', (lambda event, e = ents: fetch(e)))
            root.mainloop()

2 个答案:

答案 0 :(得分:0)

这应该让你开始,虽然显然还有更多工作要做。此程序使用实例对象/变量,因此您不会从函数返回值,因为实例对象可在类中的任何位置使用。此外,我使用grid()而不是pack(),但这只是个人偏好。

import tkinter as tk
##import MySQLdb
## from tkinter import *  import tkinter once only

class MainWindow():
    ##counter = 0         
    def __init__(self, root):
        self.t = tk.Toplevel(root)
        self.t.geometry('+1000+10')
        self.t.focus_force()
        self.t.wm_title("New Customer Registration")

        self.fields = ("Username*", "Password*", "Confirm password*",
                      "First Name*", "Last Name*", "Address 1*",
                      "Address 2 optional)", "Town/City", "Telephone Number*",
                      "Email address*", "Confirm email*")
        self.entries=[]
        self.makeform()
        wm_button1 = tk.Button(root, text = "Register", width = 15,
                            command = self.fetch)
        wm_button1.grid(row=1, column=0)
        wm_button2 = tk.Button(root, text = "Cancel", width = 15,
                            command = self.t.destroy)
        wm_button2.grid(row=2, column=0)


    def fetch(self):
        for entry in self.entries:
            field = entry[0]
            text = entry[1].get()
            print('%s: "%s"' % (field, text))
            ##update to MariaDB would go here

        self.input_frame.destroy()
        self.makeform()
        print("-"*50)


    def makeform(self):
        ##use a Frame that will be destroyed
        self.input_frame=tk.Frame(self.t)
        self.input_frame.grid()

        self.entries=[]
        this_row=0
        for field in self.fields:
            label = tk.Label(self.t, width = 20, text = field, anchor = 'w')
            entry = tk.Entry(self.t, width = 25)
            label.grid(row=this_row, column=0, sticky="nsew")
            entry.grid(row=this_row, column=1, sticky="nsew")
            this_row += 1
            self.entries.append((field, entry))

        ## change "fields" so it won't ask for Name & Password
        self.fields = ("First Name*", "Last Name*", "Address 1*",
              "Address 2 (optional)", "Town/City", "Telephone Number*",
              "Email address*", "Confirm email*")

##            not necessary, entires is an instance object
##            return entries

if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('+1000+300')
    MW=MainWindow(root)
    root.mainloop()

答案 1 :(得分:0)

import tkinter as tk
import MySQLdb
from tkinter import *

class MainWindow(tk.Frame):
counter = 0
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.background = tk.PhotoImage(file = "D:\\UoB\\Herb\\water1.png")
self.back_label = tk.Label(self, image = self.background)
self.back_label.place(x = 0, y = 0, relwidth = 1, relheight = 1)
self.button1 = tk.Button(self, text = "Customer Login", width = 20, command=self.create_window1)
self.button1.place(relx = 0.5, rely = 0.20, anchor = CENTER)
self.button2 = tk.Button(self, text = "New Customer", width = 20, command=self.create_window2)
self.button2.place(relx = 0.5, rely = 0.40, anchor = CENTER)
self.button3 = tk.Button(self, text = "Driver", width = 20,command = self.create_window3)
self.button3.place(relx = 0.5, rely = 0.60, anchor = CENTER)
self.button4 = tk.Button(self, text = "Administrator", width = 20, command=self.create_window4)
self.button4.place(relx = 0.5, rely = 0.80, anchor = CENTER)

def create_window1(self):
t = tk.Toplevel(self)
t.focus_force()
t.wm_title("Customer Login")
t.geometry("380x180+535+275")
wm_labelText = StringVar()
wm_labelText.set("Username")
wm_labelUser = Label(t, textvariable = wm_labelText)
wm_labelUser.place(x = 20, y = 30)
username = StringVar(None)
user = Entry(t, textvariable = username, width = 30)
user.place(x = 100, y = 30)
wm_passwText = StringVar()
wm_passwText.set("Password")
wm_labelPass = Label(t, textvariable = wm_passwText)
wm_labelPass.place(x = 20, y = 65)
password = StringVar(None)
passw = Entry(t, textvariable = password, width = 30, show = "*")
passw.place(x = 100, y = 65)
wm_button1 = tk.Button(t, text = "Login", width = 15)
wm_button1.place(x = 30, y = 115)
wm_button2 = tk.Button(t, text = "Cancel", width = 15, command = t.destroy)
wm_button2.place(x = 210, y = 115)

def create_window2(self):
t = tk.Toplevel(self)
t.focus_force()
t.wm_title("New Customer Registration")
fields = ("Username*", "Password*", "Confirm password*", "First Name*", "Last Name*", "Address 1*", "Address 2", "Town/City", "Telephone Number*", "Email address*", "Confirm email*")

def fetch(entries):
for entry in entries:
field = entry[0]
text = entry[1].get()
print('%s: "%s"' % (field, text))

def makeform(t, fields):
entries = []
for field in fields:
row = Frame(t)
label = Label(row, width = 20, text = field, anchor = 'w')
entry = Entry(row, width = 25)
row.pack(side = TOP, padx = 5, pady = 5)
label.pack(side = LEFT)
entry.pack(side = LEFT)
entries.append((field, entry))
return entries

if __name__ == '__main__':
root = t
root.geometry('+520+120')
ents = makeform(root, fields)
label_new = Label(root, text = "All fields marked with * are mandatory")
label_new.config(font = ('times', 10, 'bold'))
label_new.pack()
wm_button1 = Button(root, text = "Register", width = 15, command = t.register)
wm_button1.pack(side = LEFT, padx = 35, pady = 5)
wm_button2 = Button(root, text = "Cancel", width = 15, command = t.destroy)
wm_button2.pack(side = LEFT, padx = 10, pady = 5)
root.bind('<Return>', (lambda event, e = ents: fetch(e)))
root.mainloop()

def create_window3(self):
t = tk.Toplevel(self)
t.focus_force()
t.wm_title("Driver Login")
t.geometry("380x180+535+275")
wm_labelText = StringVar()
wm_labelText.set("Username")
wm_labelUser = Label(t, textvariable = wm_labelText)
wm_labelUser.place(x = 20, y = 30)
username = StringVar(None)
user = Entry(t, textvariable = username, width = 30)
user.place(x = 100, y = 30)
wm_passwText = StringVar()
wm_passwText.set("Password")
wm_labelPass = Label(t, textvariable = wm_passwText)
wm_labelPass.place(x = 20, y = 65)
password = StringVar(None)
passw = Entry(t, textvariable = password, width = 30, show = "*")
passw.place(x = 100, y = 65)
wm_button1 = tk.Button(t, text = "Login", width = 15)
wm_button1.place(x = 35, y = 115)
wm_button2 = tk.Button(t, text = "Cancel", width = 15, command = t.destroy)
wm_button2.place(x = 215, y = 115)

def create_window4(self):
t = tk.Toplevel(self)
t.focus_force()
t.wm_title("Administrator Login")
t.geometry("380x180+535+275")
wm_labelText = StringVar()
wm_labelText.set("Username")
wm_labelUser = Label(t, textvariable = wm_labelText)
wm_labelUser.place(x = 20, y = 30)
username = StringVar(None)
user = Entry(t, textvariable = username, width = 30)
user.place(x = 100, y = 30)
wm_passwText = StringVar()
wm_passwText.set("Password")
wm_labelPass = Label(t, textvariable = wm_passwText)
wm_labelPass.place(x = 20, y = 65)
password = StringVar(None)
passw = Entry(t, textvariable = password, width = 30, show = "*")
passw.place(x = 100, y = 65)
wm_button1 = tk.Button(t, text = "Login", width = 15)
wm_button1.place(x = 30, y = 115)
wm_button2 = tk.Button(t, text = "Cancel", width = 15, command = t.destroy)
wm_button2.place(x = 210, y = 115)

if __name__ == "__main__":
root = tk.Tk()
main = MainWindow(root)
root.iconphoto( root, PhotoImage( file="D:\\UoB\\Herb\\bubbles.png" ) )
root.title( "Welcome to Sky Laundry" )
root.geometry( "400x400+525+175" )
main_menu = Menu( root, tearoff=0 )
main_menu.add_command( label="Quit", command = root.destroy )
root.config( menu = main_menu )
main.pack(side = "top", fill = "both", expand = True)
root.mainloop()