hi,我正在学习使用python创建程序,但是我遇到按钮和框架问题,当我单击按钮1时,它可以正常运行,但是当我移至单击按钮2时,按钮1上的标签和列会重新出现在点击按钮2中
这是我的代码:
from tkinter import *
#import sqlite3
#import tkinter.messagebox
conn = sqlite3.connect("dbkom.db")
c = conn.cursor()
class Application:
def __init__(self, master):
self.master = master
self.master.title("Aplikasi Penjualan Komputer")
# membuat frame utama aplikasi
self.left = Frame(master, width=200, height=720, borderwidth=5, bg='white')
self.left.pack(side=LEFT)
self.top = Frame(master, width=800, height=60, bg='white')
self.top.pack(side=TOP)
self.right = Frame(master, width=800, height=700, borderwidth=5, bg='lightblue')
self.right.pack(side=RIGHT)
# Judul Program
self.heading = Label(self.top, text="Aplikasi Penjualan Komputer", font=('calibri 16 bold'), bg= 'white')
self.heading.place(x=200, y=20)
# Button 1
self.submit = Button(self.left, text="Data Pelanggan", width=20, height=2, bd = 0, bg='dimgrey', activebackground = 'lightgray',
activeforeground = 'black', fg = 'lightgray', command=self.data_pelanggan)
self.submit.place(x=25, y=100)
# Button 2
self.submit = Button(self.left, text="Data Barang", width=20, height=2, bd = 0, bg='dimgrey', activebackground = 'lightgray',
activeforeground = 'black', fg = 'lightgray', command = self.data_barang)
self.submit.place(x=25, y=150)
self.submit = Button(self.left, text="Data Servis", width=20, height=2, bd=0, bg='dimgrey', activebackground='lightgray',
activeforeground='black', fg='lightgray')
self.submit.place(x=25, y=200)
self.submit = Button(self.left, text="Cetak Faktur", width=20, height=2, bd=0, bg='dimgrey', activebackground='lightgray',
activeforeground='black', fg='lightgray')
self.submit.place(x=25, y=250)
def data_pelanggan(self):
# button1
# Membuat user input
self.noplg = Label(self.right, text="No Pelanggan", fg='black', width=13, border=0, bg='lightblue')
self.noplg.place(x=25, y=100)
self.noplg_ent = Entry(self.right, width=20)
self.noplg_ent.place(x=125, y=100)
self.namaplg = Label(self.right, text="Nama Pelanggan", fg='black', width=13, border=0, bg='lightblue')
self.namaplg.place(x=25, y=120)
self.namaplg_ent = Entry(self.right, width=20)
self.namaplg_ent.place(x=125, y=120)
self.notlpn = Label(self.right, text="No Telepon", fg='black', width=10, border=0, bg='lightblue')
self.notlpn.place(x=25, y=140)
self.notlpn_ent = Entry(self.right, width=20)
self.notlpn_ent.place(x=125, y=140)
self.alamat = Label(self.right, text="Alamat", fg='black', width=10, border=0, bg='lightblue')
self.alamat.place(x=25, y=160)
self.alamat_ent = Entry(self.right, width=20)
self.alamat_ent.place(x=125, y=160)
self.jkelamin = Label(self.right, text="Jenis Kelamin", fg='black', width=10, border=0, bg='lightblue')
self.jkelamin.place(x=25, y=180)
self.jkelamin_ent = Entry(self.right, width=20)
self.jkelamin_ent.place(x=125, y=180)
self.submit = Button(self.right, text="Input Data", width=10, height=1, bd=0, bg='yellowgreen',
activebackground='lightgray', activeforeground='black', fg='black', command = self.dataplgget)
self.submit.place(x=125, y=230)
self.submit = Button(self.right, text="Clear Data", width=10, height=1, bd=0, bg='coral',
activebackground='lightgray', activeforeground='black', fg='black', command = self.cleardataplg)
self.submit.place(x=210, y=230)
self.submit = Button(self.right, text="Show Data", width=10, height=1, bd=0, bg='yellowgreen',
activebackground='lightgray', activeforeground='black', fg='black')
self.submit.place(x=295, y=230)
def dataplgget(self):
self.val1 = self.noplg_ent.get()
self.val2 = self.namaplg_ent.get()
self.val3 = self.notlpn_ent.get()
self.val4 = self.alamat_ent.get()
self.val5 = self.jkelamin_ent.get()
# cek input data gagal dan sukses
if self.val1 == '' or self.val2 == '' or self.val3 == '' or self.val4 == '' or self.val5 == '':
tkinter.messagebox.showinfo("Warning", "Isi semua kolom")
else:
sql = "INSERT INTO 'data_pelanggan' (noplg, nm_plg, alamat, notlp, jk) VALUES(?, ?, ?, ?, ?)"
c.execute(sql, (self.val1, self.val2, self.val3, self.val4, self.val5))
conn.commit()
tkinter.messagebox.showinfo("Sukses", "Pelanggan dengan nama " + str(self.val2) + " sudah dibuatkan")
def cleardataplg(self):
self.noplg_ent.delete(0, END)
self.namaplg_ent.delete(0, END)
self.notlpn_ent.delete(0, END)
self.alamat_ent.delete(0, END)
self.jkelamin_ent.delete(0, END)
def data_barang(self):
# button 2
# User Input
self.kdbrg = Label(self.right, text="Kode Barang", fg='black', width=10, border=0, bg='lightblue')
self.kdbrg.place(x=25, y=100)
self.kdbrg_ent = Entry(self.right, width=20)
self.kdbrg_ent.place(x=125, y=100)
self.nmbrg = Label(self.right, text="Nama Barang", fg='black', width=10, border=0, bg='lightblue')
self.nmbrg.place(x=25, y=120)
self.nmbrg_ent = Entry(self.right, width=20)
self.nmbrg_ent.place(x=125, y=120)
self.hrgbrg = Label(self.right, text="Harga Barang", fg='black', width=10, border=0, bg='lightblue')
self.hrgbrg.place(x=25, y=140)
self.hrgbrg_ent = Entry(self.right, width=20)
self.hrgbrg_ent.place(x=125, y=140)
self.stokbrg = Label(self.right, text="Stok Barang", fg='black', width=10, border=0, bg='lightblue')
self.stokbrg.place(x=25, y=160)
self.stokbrg_ent = Entry(self.right, width=20)
self.stokbrg_ent.place(x=125, y=160)
self.submit = Button(self.right, text="Input Data", width=10, height=1, bd=0, bg='yellowgreen',
activebackground='lightgray', activeforeground='black', fg='black')
self.submit.place(x=125, y=230)
self.submit = Button(self.right, text="Clear Data", width=10, height=1, bd=0, bg='coral',
activebackground='lightgray', activeforeground='black', fg='black', command=self.cleardatabrg)
self.submit.place(x=210, y=230)
self.submit = Button(self.right, text="Show Data", width=10, height=1, bd=0, bg='yellowgreen',
activebackground='lightgray', activeforeground='black', fg='black')
self.submit.place(x=295, y=230)
def cleardatabrg(self):
self.kdbrg_ent.delete(0, END)
self.nmbrg_ent.delete(0, END)
self.hrgbrg_ent.delete(0, END)
self.stokbrg_ent.delete(0, END)
# membuat objek
r = Tk()
b = Application(r)
# mengubah logo
#r.iconbitmap('logokom.ico')
# mengatur resolusi dan tampilan awalan
r.geometry("800x400+180+180")
# preventif resolusi
r.resizable(False, False)
# akhir dari loop
r.mainloop()
我想,当我点击按钮2时,点击按钮1的结果没有被带走
答案 0 :(得分:0)
这两个选项都会使小部件消失。我不知道任何一个的后果。也许要等别人发表评论/回答,或者问一个单独的问题来询问结果(或研究/阅读文档)。
self.jkelamin.destroy()
self.jkelamin_ent.destroy()
或
self.jkelamin.place_forget()
self.jkelamin_ent.place_forget()
Tkinter - place_forget command
The Tkinter Place Geometry Manager