我有点不解决这个问题。我正在尝试构建一个包含多个页面的应用程序。为此,我创建了一个主框架然后在每个时刻提升我需要的框架。这是我现在写的代码:
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
main_container = tk.Frame(self)
main_container.grid(sticky = "nsew")
main_container.grid_rowconfigure(0, weight = 1)
main_container.grid_columnconfigure(0, weight = 1)
menu_bar = tk.Menu(main_container)
file_menu = tk.Menu(menu_bar, tearoff = 0)
file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
file_menu.add_separator()
file_menu.add_command(label = "Exit", command = quit)
menu_bar.add_cascade(label = "File", menu = file_menu)
tk.Tk.config(self, menu = menu_bar)
self.frames = {}
for fr in (MainPage, GraphsPage, Page2):
frame = fr(main_container, self)
self.frames[fr] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(MainPage)
def show_frame(self, pointer):
frame = self.frames[pointer]
frame.tkraise()
然后,MainPage,例如:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
#Tried this but it doesn't work
#self.columnconfigure(0, weight = 1)
#self.rowconfigure(0, weight = 1)
#self.rowconfigure(1, weight = 1)
#self.rowconfigure(2, weight = 1)
#self.rowconfigure(3, weight = 1)
label = tk.Label(self, text = "Main Page", font = LARGE_FONT)
label.grid(row = 0, padx = 10, pady = 10)
button1 = ttk.Button(self, text = "Graphs", command = lambda: controller.show_frame(GraphsPage))
button1.grid(row = 1, sticky = 'nswe')
button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
button2.grid(row = 2, sticky = 'nswe')
button3 = ttk.Button(self, text = "Exit", command = quit)
button3.grid(row = 3, sticky = 'nswe')
(为简单起见,我没有显示GraphsPage和第2页)。最后,我将程序运行为:
app = Main()
app.geometry("1280x720")
app.mainloop()
我能够使按钮适合整列,但列不适合“1280x720”主容器。我错过了什么? 提前感谢您的时间。
答案 0 :(得分:2)
你在MainPage
构造函数中有正确的想法,但随后对其进行了评论,但是您错过了在顶级Tk
对象中执行此操作:
import tkinter as tk
from tkinter import ttk
LARGE_FONT = ("ariel", 20) # dont know what you had here
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.grid_rowconfigure(0, weight=1) # this needed to be added
self.grid_columnconfigure(0, weight=1) # as did this
main_container = tk.Frame(self)
main_container.grid(column=0, row=0, sticky = "nsew")
main_container.grid_rowconfigure(0, weight = 1)
main_container.grid_columnconfigure(0, weight = 1)
menu_bar = tk.Menu(main_container)
file_menu = tk.Menu(menu_bar, tearoff = 0)
file_menu.add_command(label = "Save settings", command = lambda: popupmsg("Not supported yet!"))
file_menu.add_separator()
file_menu.add_command(label = "Exit", command = quit)
menu_bar.add_cascade(label = "File", menu = file_menu)
tk.Tk.config(self, menu = menu_bar)
self.frames = {}
for fr in (MainPage,):
frame = fr(main_container, self)
self.frames[fr] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(MainPage)
def show_frame(self, pointer):
frame = self.frames[pointer]
frame.tkraise()
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# uncommented these lines
self.columnconfigure(0, weight = 1)
self.rowconfigure(0, weight = 1)
self.rowconfigure(1, weight = 1)
self.rowconfigure(2, weight = 1)
self.rowconfigure(3, weight = 1)
label = tk.Label(self, text = "Main Page", font = LARGE_FONT)
label.grid(row = 0, padx = 10, pady = 10)
button1 = ttk.Button(self, text = "Graphs", command = lambda: controller.show_frame(GraphsPage))
button1.grid(row = 1, sticky = 'nswe')
button2 = ttk.Button(self, text = "Page 2", command = lambda: controller.show_frame(Page2))
button2.grid(row = 2, sticky = 'nswe')
button3 = ttk.Button(self, text = "Exit", command = quit)
button3.grid(row = 3, sticky = 'nswe')
app = Main()
app.geometry("1280x720")
app.mainloop()
答案 1 :(得分:1)
为澄清起见,GUI无法填充整个窗口的原因是因为public class SMS extends AppCompatActivity {
private TextView header;
private Button sendbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
header = (TextView) findViewById(R.id.headerText);
sendbutton = (Button) findViewById(R.id.button);
sendbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int permissionCheck = ContextCompat.checkSelfPermission(SMS.this, Manifest.permission.SEND_SMS);
if( permissionCheck == PackageManager.PERMISSION_GRANTED) {
MyMessage();
} else {
ActivityCompat.requestPermissions(SMS.this, new String[]{Manifest.permission.SEND_SMS}, 0);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 0:
if (grantResults.length >= 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
MyMessage();
} else {
Toast.makeText(getApplicationContext(), "Permission needed to send SMS", Toast.LENGTH_SHORT).show();
}
break;
}
}
private void MyMessage() {
//SharedPreferences result = getSharedPreferences("saveddata", MODE_PRIVATE);
SharedPreferences result = getSharedPreferences("saveddata", MODE_PRIVATE);
String savedname = result.getString("NAME", "name not found");
String savedphone = result.getString("PHONE", "phone not found");
String message = "I need help" + " " + savedname ;
if (!savedphone.equals("")) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(savedphone, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Phone number or name n.a.", Toast.LENGTH_SHORT).show();
}
}
}
(您的主要Tkinter对象)未分配权重。在Tkinter中,weight描述了如果有多余的空间,行或列将增长多少。默认情况下,Tkinter中的所有内容的权重均为0,因此除非您修改内容,否则不会调整任何大小。