【问题标题】:how do I implement the result of 2 buttons in 1 frame in a python program?如何在 python 程序中实现 1 帧中 2 个按钮的结果?
【发布时间】:2019-05-02 13:38:50
【问题描述】:

嗨,我正在学习用 python 创建一个程序,但是我有一个按钮和框架问题,当我单击按钮 1 时它运行正常,但是当我移动到单击按钮 2 时,按钮 1 上的标签和列重新出现在点击按钮 2

Problem when click butoon 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 的结果没有被带走

【问题讨论】:

  • 对不起,也许回到原来的。您需要全部(除了数据库连接和图标位图)来复制问题。
  • 尚不清楚单击按钮 2 时您期望发生什么。
  • 在“data pelanggan”按钮上我有 6 列,在“data barang”按钮上我有 4 列,当我在“data pelanggan”按钮上完成输入数据时,在下一个按钮上 [ data barang] 列增加到 6 列,如何解决?
  • 您是说希望jkelaminjkelamin_ent 小部件在“Data Barang”按钮打开时消失消失点击了吗?
  • 我想在点击“Data Barang”按钮时消除 jkelamin 和 jkelamin_ent。有什么想法吗?

标签: python tkinter


【解决方案1】:

其中任何一个都会使小部件消失。我不知道两者的后果。也许等待其他人评论/回答或提出单独的问题以询问后果(或研究/阅读文档)。

        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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2016-08-07
    • 2017-11-04
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多