- Joined
- Mar 18, 2018
- Posts
- 653
- Reaction
- 666
- Points
- 357
patulong naman po jan sa mga programmer baka may alam po kayo kung pano iseperate yung code gamit ang dalawang saved file na naka project. please need lang po talaga.
salamat ts class module nga hahaha. ayos nasya binigyan kame code nung prof namen. kaso dikopa masyadong gets i apply ko sa na calculator at converter hahahaclassmodules ata gusto mo mangyari ts
from tkinter import *
#ngayon lagay mo na Function!! maya na natin gawin GUI! HAHAH! heto sample
#calculator to!HAHAHA
def taee_baho(num):
global expression
#ngayon combine natin mga string pati mga number at operators!
expression = expression + str(num)
equation.set(expression)
#ngayon hetong function para i evaluate ung final expression!
def egualpress():
#gamitin natin ung try para if mag error ung first function antin mag generate sya ng iba!
try:
global expression
#ngayon evaluate natin ung expression pati ung str function para ibigay ung result as string
total = str(eval(expression))
equation.set(total)
#ngayon initialize natin ung variable gamit ung empty string
expression = ""
#kung mag error to gegenrate nya is execept block!
except:
equation.set("hakdog error")
expression = ""
#gawa tayo function para clear ung context
def baho_kilili(): #<--- dont worry cllear nya ung text kahit ganyan ung name nya HAHAH
global expression
expression = ""
equation.set("")
#taena tatawa ako sa definition HAHAHAH! SO HETO UNG FIRST CODE MODULE AHHAH!
if __name__ == "__main__":
#gawa tayo ng GUI di muna ako mag lagay comment hahha! para mabilis
baho_paa = Tk()
baho_paa.configure(background="light blue") #<-heto ung kulay ng background
baho_paa.geometry("300x150") #<- heto ung size ng object
baho_paa.title("bahoHiniga") #<- heto ung title ng window HAHAH!
equation = StringVar()
expression_field = Entry(baho_paa, text=equation)
expression_field.grid(columnspan=4, ipadx=70)
equation.set('Lagay mo Ano mabaho jk') <- laay mo ung expreesssion!
button1 = Button(baho_paa, text=' 1 ', fg='black', bg='red',
command=lambda: press(1), height=1, width=7)
button1.grid(row=2, column=0)
button2 = Button(baho_paa, text=' 2 ', fg='black', bg='red',
command=lambda: press(2), height=1, width=7)
button2.grid(row=2, column=1)
button3 = Button(baho_paa, text=' 3 ', fg='black', bg='red',
command=lambda: press(3), height=1, width=7)
button3.grid(row=2, column=2)
button4 = Button(baho_paa, text=' 4 ', fg='black', bg='red',
command=lambda: press(4), height=1, width=7)
button4.grid(row=3, column=0)
button5 = Button(baho_paa, text=' 5 ', fg='black', bg='red',
command=lambda: press(5), height=1, width=7)
button5.grid(row=3, column=1)
button6 = Button(baho_paa, text=' 6 ', fg='black', bg='red',
command=lambda: press(6), height=1, width=7)
button6.grid(row=3, column=2)
button7 = Button(baho_paa, text=' 7 ', fg='black', bg='red',
command=lambda: press(7), height=1, width=7)
button7.grid(row=4, column=0)
button8 = Button(baho_paa, text=' 8 ', fg='black', bg='red',
command=lambda: press(8), height=1, width=7)
button8.grid(row=4, column=1)
button9 = Button(baho_paa, text=' 9 ', fg='black', bg='red',
command=lambda: press(9), height=1, width=7)
button9.grid(row=4, column=2)
button0 = Button(baho_paa, text=' 0 ', fg='black', bg='red',
command=lambda: press(0), height=1, width=7)
button0.grid(row=5, column=0)
plus = Button(baho_paa, text=' + ', fg='black', bg='red',
command=lambda: press("+"), height=1, width=7)
plus.grid(row=2, column=3)
minus = Button(baho_paa, text=' - ', fg='black', bg='red',
command=lambda: press("-"), height=1, width=7)
minus.grid(row=3, column=3)
multiply = Button(baho_paa, text=' * ', fg='black', bg='red',
command=lambda: press("*"), height=1, width=7)
multiply.grid(row=4, column=3)
divide = Button(baho_paa, text=' / ', fg='black', bg='red',
command=lambda: press("/"), height=1, width=7)
divide.grid(row=5, column=3)
equal = Button(baho_paa, text=' = ', fg='black', bg='red',
command=equalpress, height=1, width=7)
equal.grid(row=5, column=2)
clear = Button(baho_paa, text='Clear', fg='black', bg='red',
command=clear, height=1, width=7)
clear.grid(row=5, column='1')
baho_paa.mainloop()
thank you paps. may own method nako pero diko maseperate. gamit is tkinter namay self.1. separate the variable and function amd class method.Python:from tkinter import * #ngayon lagay mo na Function!! maya na natin gawin GUI! HAHAH! heto sample #calculator to!HAHAHA def taee_baho(num): global expression #ngayon combine natin mga string pati mga number at operators! expression = expression + str(num) equation.set(expression) #ngayon hetong function para i evaluate ung final expression! def egualpress(): #gamitin natin ung try para if mag error ung first function antin mag generate sya ng iba! try: global expression #ngayon evaluate natin ung expression pati ung str function para ibigay ung result as string total = str(eval(expression)) equation.set(total) #ngayon initialize natin ung variable gamit ung empty string expression = "" #kung mag error to gegenrate nya is execept block! except: equation.set("hakdog error") expression = "" #gawa tayo function para clear ung context def baho_kilili(): #<--- dont worry cllear nya ung text kahit ganyan ung name nya HAHAH global expression expression = "" equation.set("") #taena tatawa ako sa definition HAHAHAH! SO HETO UNG FIRST CODE MODULE AHHAH!
so this is a samplee! rename ur file first into two mod!
PS: dapat nasa same folder ssila
main.py <- here the 1st mode
gui_shit.py
wait gawin ko muna ung gui HAHAH!
from tkinter import END
from tkinter import StringVar
import tkinter
import tkinter.messagebox
class myGUI:
def __init__(self):
self.root = tkinter.Tk()
self.root.title("CALCULATOR")
#myvar
self.UserIn = tkinter.StringVar()
#myframes
self.topFrame = tkinter.Frame(self.root)
self.secondFrame = tkinter.Frame(self.root)
self.thirdFrame = tkinter.Frame(self.root)
self.fourthFrame = tkinter.Frame(self.root)
self.fifthFrame = tkinter.Frame(self.root)
self.sixthFrame = tkinter.Frame(self.root)
self.entry = tkinter.Entry(self.topFrame, width = 25, justify = "right", \
borderwidth = 5, bg = "skyblue", textvariable = self.UserIn)
self.entry.insert (0, "0")
self.task = ""
#mybuttons number
self.button_1 = tkinter.Button(self.secondFrame, text = "1", bg = "black", \
borderwidth = 3, padx = 20, pady = 20, fg = "white", \
command = lambda: self.button_click(1))
self.button_2 = tkinter.Button(self.secondFrame, text = "2", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(2))
self.button_3 = tkinter.Button(self.secondFrame, text = "3", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(3))
self.button_4 = tkinter.Button(self.thirdFrame, text = "4", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(4))
self.button_5 = tkinter.Button(self.thirdFrame, text = "5", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(5))
self.button_6 = tkinter.Button(self.thirdFrame, text = "6", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(6))
self.button_7 = tkinter.Button(self.fourthFrame, text = "7", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(7))
self.button_8 = tkinter.Button(self.fourthFrame, text = "8", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(8))
self.button_9 = tkinter.Button(self.fourthFrame, text = "9", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(9))
self.button_0 = tkinter.Button(self.fifthFrame, text = "0", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda: self.button_click(0))
#buttons operator
self.button_ADD = tkinter.Button(self.secondFrame, text = "+", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda : self.button_click("+"))
self.button_SUB = tkinter.Button(self.thirdFrame, text = "-", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda : self.button_click("-"))
self.button_MUL = tkinter.Button(self.fourthFrame, text = "*", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda : self.button_click("*"))
self.button_DIV = tkinter.Button(self.fifthFrame, text = "/", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda : self.button_click("/"))
self.button_CLR = tkinter.Button(self.fifthFrame, text = "C", bg = "red", \
borderwidth = 3, padx = 20, pady = 20, \
command = self.ClearDisplay)
self.button_DOT = tkinter.Button(self.fifthFrame, text = ".", bg = "black", fg = "white", \
borderwidth = 3, padx = 20, pady = 20, \
command = lambda : self.button_click("."))
self.button_EQU = tkinter.Button(self.sixthFrame, text = "=", bg = "gray", \
borderwidth = 3, padx = 20, pady = 15, \
width = 25, command = self.Calculation)
#entry
self.entry.pack()
#buttons
self.button_1.pack(side = "left")
self.button_2.pack(side = "left")
self.button_3.pack(side = "left")
self.button_4.pack(side = "left")
self.button_5.pack(side = "left")
self.button_6.pack(side = "left")
self.button_7.pack(side = "left")
self.button_8.pack(side = "left")
self.button_9.pack(side = "left")
self.button_CLR.pack(side = "left")
self.button_0.pack(side = "left")
#buttons of operators on pack
self.button_ADD.pack(side = "left")
self.button_SUB.pack(side = "left")
self.button_MUL.pack(side = "left")
self.button_DOT.pack(side = "left")
self.button_DIV.pack(side = "left")
self.button_EQU.pack(side = "left")
#myframes on pack
self.topFrame.pack()
self.secondFrame.pack()
self.thirdFrame.pack()
self.fourthFrame.pack()
self.fifthFrame.pack()
self.sixthFrame.pack()
tkinter.mainloop()
def button_click(self, number):
self.task = str(self.task) + str(number)
self.UserIn.set(self.task)
def Calculation(self):
self.data = self.entry.get()
try:
self.answer = eval(self.data)
self.displayText(self.answer)
self.task = self.answer
except SyntaxError:
self.displayText("Invalid Syntax!")
self.task = ""
def displayText(self, value):
self.entry.delete(0, END)
self.entry.insert(0, value)
def ClearDisplay(self):
self.task = ""
self.entry.delete(0, END)
self.entry.insert(0, "0")
myGUI()
student palang po kasi ako hahaha. need daw poyun nang prof namen so if hindi namin naseperate yung tatlong project, wala din kwenta pinagpaguran hahahaha. baka naman poehh hindi mo nman po kelangan ilagay ung stringVAR lol meron pa isang method para maag design depende na kasi yan sa prgorammer :at ung tkinter END lol! hahha at saka bat mo pa kelangan gawin into diffrent files kung pwd nman as one hAhahha